helpful-notes



  
 
 

wscite does not recognize environment variables

 
The wscite editor is a simple efficient tool to support introductory level exercises in programming for example in java. I have encountered a problem with setting the environment variable like PATH and CLASSPATH. I did not want to set those variables statically by hand through the control panel, so I wrote a simple script setenvironment.cmd and hoped to work in the following way.
  1. open console window by cmd
  2. run setenvironment.cmd
  3. open wscite
  4. edit source file test.java
  5. perform command compile
  6. perform command go

It did not work: command compile which is performed as

javac $(FileNameExt)

failed with the error message that javac is not recognized as a command although the PATH variable set by the setenvironment.cmd included the correct path to javac.exe.

Apparently wcsite opened in the console after the setenvironment.cmd was run did not inherited the proper environment. Experimenting for quite some time I have finally found the cure: the commands should be performed as

cmd /c javac $(FileNameExt)

The simple way how to arrange it is to edit the local options file SciTEUser.properties which should look like

command.compile.*.java=cmd /c javac $(FileNameExt)

command.build.*.java=cmd /c javac *.java
command.go.*.java=cmd /c java $(FileName)

The file can be copied from here SciTEUser.properties

Note added: later I found that the problem was in using quotes " in the PATH variable to to write (because of space in Program Files)
set JDK="C:\Program Files\java\jdk1.6.0_10"
set PATH=%JDK%\bin\;%PATH%

javac must be called through cmd /c construction when quotes are used. Without quotes, that is like
set JDK=C:\Program Files\java\jdk1.6.0_10
set PATH=%JDK%\bin\;%PATH%

scite works well.

total commander not showing file size in bytes

 
wincmd.ini should contain the line
SizeStyle=0
 
 

Free Eset on-line scanner does not run in iexplorer in Vista

 

Error message "should run uder administrator privileges" even if one is logged on as admin. Cure: one should open iexplorer clicking  by the right mouse button and choose "run as administrator"


Bootable BartPE CD does not see the local HD

 

 

I am using Macrium Reflect backup software which in cooperation with Bart PE produces bootable rescue Windows live CD to enable HDD restoration from disk image. My Lenovo notebook boots with the rescue CD but the local hard disk is not recognized (the UBS drives are ok). It helped to change the BIOS settings for the hard disk: to disable the AHCI enabled system. When AHCI is disabled, the BartPE recognizes the disk and even the Lenovo service partition is seen.

eFilmlt dicom viewer does not run under Vista

 
Very often CT and other medical images  are burned to CD and given to patient (and to his doctor) accompanied by the eFilmlt dicom viewer. It happens that this software does not run under Vista. In such case, one can use for example this free dicom viewer

How to run NetBeans in order the tested application sees the run-time support dll's

In Windows:

Method 1. Onen cmd window, set PATH environment variable to include paths to the dll's. Then run NetBeans from the command line, it inherits PATH
 
Method 2. Open project Properties -> Run -> VM parameters and add the systemproperty via -Djava.llibrary.path=requireddlllibrary
The virtual machine will run with the set system property

 

Xming on Vista

It seems the port 6000 needed by default by Xming x-server is not free in Vista. The solution is to use the display > 0, e.g. set to 10 by Xlaunch


and then running putty (ssh) tunneling X11, one should specify the display number as 10 as well

sorry: typo in the figure, should read localhost:10


Tunelling vnc by ssh


I descreibe here a particular situation: remote machine is linux, local machine windows. I want to run the vnc server on the remote machine and vncviewer on the local machine and for security i want to tunnel it through ssh.

1.On rermote machine run   
                    vncserver -localhost
   this forbids remote acces to vnc, only through loopback. Remember the display number assigned by vncserver, say it will be 3. This means that the vnc server
   expects communication on the port 5900+3 = 5903

2. On local machine run putty and configure tunneling as follows (source port: 5900, destination: localhost:5903)


3. on local machine run vncviewer as


One gets a simple x-windows window manager with x-terminal open. One can even start full session manager like gnome. In ubuntu just run in the terminal
x-session-manager. One gets the full gnome environment.


Java modal windows, blocking mouse events, getting window to top



Using modal dialog windows in Java is relatively easy with predefined dialogs in JOptionPane. However some problems appear:
  • when the modal dialog window is displayed on the screen, its parent (calling) window still accepts events (from mouse and keyboard) and pushes those events into the event que. When the dialog window is closed those events are dispatched and maybe this was not the intention of the user who just accidentally clicked the parent window component.
  • it is not a good practice to perform long tasks under the event dispatching thread, since the user might get impression that the application does not respond. One can change the default cursor to busy cursor, but this does not block the mouse events being pushed into event que and the actions corresponding of those events are fired after the current dispatching thread task ends. This was most probably not the intention of a nervous user who started randomly clicking the buttons having the feeling that the application does not respond even if it displays the busy cursor. Even worse, when a modal dialog window is opened and then closed during the processing of the current event, the busy cursor of the parent window is restored to the default cursor, even if the current event process has not ended yet. (This is a well known Java bug.). Even if one correctly uses the swing Worker to perform the background task, one still  may want to  forbid events on the parent window till the worker ends its work. If there are many controls within the windows, disabling all of them is tedious and non universal work.
  • when the dialog window is closed, one naturally expects that the focus is returned to the parent (calling) window, which is often really true, but not under every window manager. Thewre are window managers which pop to top just arbitrary window. And if this popped-up window is large, it can hide the original calling window below, what look as if the original window was disposed together with the closing dialog window and the user have impression that the application crashed. For example in Linux the mwm manager behaves in this ugly way, while the twm manager does not suffer from this problem. The effect looks random: the parent window sometimes does get the focus and gets to top, sometimes gets hidden below the other winodws (not only of the current Java application, but below any random window on the screen.
  • the same problem of not getting the right window to the top (disappearing window) happens also when I have some window active and open a new (non modal) window (for example by pressing a button in the active window). The new window behaves just randomly (under some window managers). That is the new window sometimes correctly appears on top of of the windows and gets focus, while sometimes it disappears hidden below of the opened windows on the screen.
I do not know about any official cure to those related problems. There are many suggestions on web, which do not really work in each situation. This note is, maybe, another note of this type, proposing a cure which is not a true universal cure. But I found it almost working (not 100 percent cure) although I do not quite understand why it should work. Even worse, I found that I have to introduce some arbitrary waiting loops into the code which, for sure, must be machine dependent. I found the delays in those loops just by trial and error on my machines until I statistically observed acceptable behavior. This is certainly bad practice, but not knowing anything better, I am satisfied with the behavior. If anybody knows better really working solutions, please bring it to my attention by writing to cernyv@gmail.com

Here is the code which demonstrate the usage of my workarounds. First I define the class MyFrame as an extension of JFrame and then I use it in a simple application defined as MainFrame.

file MyFrame.java:
............................................................................................................................
import javax.swing.*;
import java.awt.event.MouseAdapter;

public class MyFrame extends JFrame{
    public JPanel glass; //to be initiated to be the glass pane of MyFrame
    private MouseAdapter mouseCatcher;
    public boolean busy;
    public JLabel jl;

    public MyFrame(){
        glass=(JPanel)this.getGlassPane();//glasspane is in fact created as JPanel
        glass.add(new JLabel("Working"));
        glass.setOpaque(true);//if set visible, glass pane covers the whole frame area
       
        mouseCatcher=new MouseAdapter(){};
        busy = false;
    }
    /**
     * waits for nmilis miliseconds
     */
    public static void waitmilis(int nmilis){
        long t0,t1;
        t0=System.currentTimeMillis();
        do{
            t1=System.currentTimeMillis();
        }
        while (t1<t0+nmilis);

    }
    public void setBusy(boolean busy){
        if(busy){
            if(this.busy) return;
            glass.addMouseListener(mouseCatcher);//catches mouse events on glass pane, the events do not
                                                                       //go through to the components below
            glass.setVisible(true);//glass is opaque!
            this.busy=true;
            paint(getGraphics());//this updates the glass pane immediately
                                        //even if we are inside event disp. thread
            return;
        }
        else{
            if(!this.busy)return;
            glass.removeMouseListener(mouseCatcher);
            glass.setVisible(false);
            this.busy=false;
            paint(getGraphics());
            return;
        }
    }
  
    /** request focus with abraca dabra delays to overcome top window bug
     *  in java which manifest itself in som window managers (in those managers
     *  arbitrary window might get on top, so one should explicitely aask the
     *  correct window to get to top and get focus, but simple requestFocus
     *  does not work
     *
     */
    public void MyRequestFocus(){
        //I do notcompletely understand wheteher evrything is necessary,
        //but it works
        super.setVisible(true);
        super.setAlwaysOnTop(true); //not sure whether this is really necessary
        super.toFront();
        waitmilis(200);   //should be adjusted by trial and error
        super.requestFocus();
        waitmilis(350);   //should be adjusted by trial and error
        super.setAlwaysOnTop(false);
    }

...........................................................................................................................
file MainFrame.java:
............................................................................................................................
import javax.swing.*;

public class MainFrame extends MyFrame {
    private javax.swing.JButton DoButton;
    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
    }

    private void initComponents() {

        DoButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        DoButton.setText("Do");
        DoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DoButtonActionPerformed(evt);
            }
        });
        getContentPane().add(DoButton);
        DoButton.setBounds(150, 120, 70, 25);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
    }

    private void DoButtonActionPerformed(java.awt.event.ActionEvent evt) {
        setBusy(true);
        new SwingWorker<Void,Void>() {
            @Override
            public Void doInBackground(){  //doing dummy work for illustration purposes
                waitmilis(2000);
                JOptionPane.showMessageDialog(null, "Half work done");   //modal dialog this makes problems
                                                                         //for some window managers
                MyRequestFocus();
                waitmilis(2000); //continue to do dummy work
                return null;    //required since return type is Void not void
            }
            protected void done(){
                setBusy(false);
            }
        }.execute();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MainFrame mf = new MainFrame();
                mf.setVisible(true);
                mf.MyRequestFocus();//to be sure the windows apperars on top
            }
        });
    }
}
................................................................................................

Virtual Box mouse integration problems


  1. Mouse integration in virtual box used to work, but now does not. Possible problem: kernel was updated and after each kernel update the VBox additions have to be installed anew.
  2. Trying to install VBox additions in VirtualBox Linux one gets error: /bin/sh: bad interpreter: Permission denied. Possible solution: CD was mounted  as noexec (check /etc/mtab). So either arrange the CD to be mounted without noexec or copy the CD content to (virtual box) hard disk and run from there. Most probably you get another error (running autorun.sh): gksu not found. So either install gksu or run directly executable like VBoxLinuxAdditions-amd64.run which finally installs the VBox additions. Do not forget to reboot, only then mouse integration becomes active.



How to tunnel subversion through ssh in Windows


plink.exe is the command line version of putty

  1. Download plink.exe from the putty home page http://www.chiark.greenend.org.uk/~sgtatham/putty/download.htm  plink.exe is portable, needs no instalation, just copy it somewhere
  2. Edit the file c:\Documents and Settings\user\Application Data\Subversion\config   :  find the section [tunnels] and add a line use slashes not backslash even if it is in windows !!
          ssh=c:/path/to/plink.exe -v -pw password                 
  1. now you can try that it works:
  2. svn ci svn+ssh://user@server/home/svn/repos/dir .
Perhaps it is better not to keep open password in the config file all the time, edit it just for the moment you need it.
There is an alternative authentication through RSA keys, it is described in http://agateau.wordpress.com/2007/07/03/windows-svnssh-and-the-subversion-command-line-client/


Autoformat of date and date-like input in Excel


Excel tries to help users by autoformating input of date. This help, however, is very annoying, since it autoformats as date also inputs which are meant as text but have similar structure as date. For example if I enter 5/5/10 it changes (within my locale) to 5.5.2010, changing the format of the cell from General to Date. The problem is, that even (within my locale) the structure 5/5/10 by no means resembles a date, Excel insists to be helpful. There is no way how to switch off this annoying property off, no completely satisfying solution exists as far as I know.

This happens always when one inputs into a cell with the format declared as General (before input). It helps, of course, to change the format of the cell to Text before input, then the cell is correctly displayed as 5/5/10, however, with the annoying "green triangle warning" that the cell contains incorrect data. What is even much worse, when you later want to change the text data by "find and replace" technique and you let change 10 to 20, you will find that it changes the cell into 5.5.2020 and even more it changes the original Text format of the cell into Date!

The default format of cells of an empty spreadsheet is General, so any input is endangered by this "user friendly" behaviour. It is of some help to  change the format of all the cells before any input to Text (select the whole table by clicking the upper left corner of the table and change the format to Text). This, however, does not protect you before the crazy behavior of Find-Replace.

There is another way to overcome the autoformating behavior. You should start any input into a cell by the character ' (apostrophe). This is interpreted as a special character and after completing the input it is not displayed in the cell. But it remains part of the data structure in the cell and you can see the apostrophe in the formula bar when you click the cell. The format of the cell remains to be General and the hidden apostrophe even protects you from the above described gangster behavior of Find-Replace. You pay by extra typing and cells decorated with green warning triangles which you have to switch off.

Why Microsoft does not provide a way to switch-off the autoformating of dates is beyond my comprehension




How to inherit aliases (bash)

A new daughter shell inherits from its parent shell the environment variables but not aliases, which could be useful. There is a simple trick how to do it.
First run in the parent shell
alias >aliases
This copies all the parent shell aliases into the file aliases
Then open the new shell like
xterm &
and then in the new shell run
source aliases
These creates aliases as they were defined int the parent shell
you can clean the working directory by
rm aliases



Nohup redirection with exit code capture


Suppose we have an executable test and we want to run it in background and to continue to run it after we log off (like a demon). But we want to log messages from the running program, so we have to redirect output, both stdout and stderr. One finds in many places how to do it:

nohup test </dev/null &>log.out &

Almost good, but not quite. The problem is that we shall see stdout and stderr messages in the log file log.out (when we login back), but if the run ends by some fatal error like "Segment violation" we shall not see any message like that in the log.out file. The reason is, that the message "Segmentation fault" which we would see had we run the program interactively in the terminal window, is not a message printed by our program to stderr. It is the exit code of the program which got killed by the signal SIGSEGV. The exit code is normally catched and printed by the shell which ran the program . But since we logged off after starting the program in the background, there is nobody to catch the exit code. After we login back, we just find that the program is no more running, but reading the log file we do not see why it (unexpectedly) stopped.

There is a workaround: to run the program test from a shell script and redirect the output from the shell script to a log file.

Here is a short shell script run.sh:
.....................................run.sh......................
#!/bin/bash
test
echo Exit code:  $?
....................................................................
The script (do not forget to chmod to enable running it) is then run via nohup as

nohup run </dev/null &>log.out &

In the log.out file you will find the output and error messages of the program test as well as the error message by the shell which is repeated (in a different form) by the echo $? command at the end of the script


linux: live view of file updates


one can follow updates to file viewed by less. On running less one presses F and less goes into "waiting for data" mode following updates in a similar way to tail -f command. The advantage is that pressing Ctrl-C less goes back to viewing mode and one can scroll the file, which is not possible for tail -f. Pressing F less goes again to "waiting for data mode"




linux: flushing printf problem


printf normally flushes the output when prints \n char, but only if writing to console. If output is redirected,  then it does not flush until requested explicitely by fflush or when the program is exiting.

However if printf is interleaved with cout then all the waiting printfs are flushed out before cout is flushed. cout is flushed immediately.




Java: working with unsigned bytes


Java byte is interpreted as signed number and there is nothing like unsigned byte. We need, however, to work with unsigned bytes for example if reading binary files. Effectively signed bytes can be used to manipulate as if unsigned bytes when we take some precautions.
For example the code
byte b = 0xFA;
gives compile error, but the following is compiled
byte b = (byte)0xFA;
Here binary reprezentation of 0xFA is put into 8 bits of b, just the interpretation for outer world is as signed byte
So
System.out.println(b);
outputs -16
However
System.out.println(b & 0xFF);
outputs 240 (what is insigned 0xFA).
So the basic trick is to use
b & 0xFF
when I want the byte value to be interpreted as unsigned byte
The reason is that binary operators +,-, &,... in Java do not operate with bytes (nor shorts): arguments are first promoted to ints
For example:
int i = b;
System.out.ptrintln(i);
outputs -16, but
int i = b & 0xFF
System.out.println(i);
outputs 240
Behind all of this is the way how negative numbers are coded.


Relative addresses of actions in powerpoint


There is a problem (bug?) in powerpoint with relative addressing of either URL's or RUN's for action shape buttons in powerpoint. Simply relative addressing does not work.
A way out is not to use prepared action buttons but rather to construct own buttons (from drawing shapes like rectangles). To such shapes one can also add hyperlink. But as a difference to provided action buttons where hyperlink and action lead to the same dialog which does not like relative addressing, hyperlink added to geometric sahpe (or a text) opens a dialog, which generates (or accepts) relative addressing. So moving one's ppt presentation together with all the links in the same relative positions (best is to have all the linked files in the same directory as the ppt file) one gets working (not broken) links in the new position.








Comments