25 Aug 2012

Sending java compiler messages to a file


javac is standard tool to compile the java source files. Successful compilation will result in bytecode whereas the unsuccessful compilation will result in the error message(s). By default all  error messages goes to System.err (which is console window).


javac has number of standard and non-standard optins which you can supply at the time of compiling a file.The standard options are those options which a supported in current jdk and will also be supported in future releases.The non-standard options are those options which are supported in current jdk but subject to change in future releases.

One of non-standard option is -Xstdout filename ,this option sends the compiler messages to the specified file rather than sending to System.err (which is console window). filename is a name of file with extension. It  could be any text file (poi.txt). Interesting thing is one can even specify filename as a html file (poi.html). You can experiment with other files extensions.
The specified file may or may not be present. If file is not present then specified file will be created in current directory and compiler messages would be sent to that file.

Lets walk with code :
In following class Stdout.java  there is two errors in line 6 and 8. Error in line 6 is becuse of local varuable "name"is not being initialized. Error in line 8 is because of a Exception object is neither handled nor declared to be thrown.


Now compile Stdout.java with -Xstdout filename option. The poi.txt is file which will contain the compiler messages. At the time of issuing following command poi.txt is not present that means it will be created.

           cmd>javac -Xstdout poi.txt Stdout.java


And here is the content of poi.txt file consisting two error messages.
             

It  is fun to find out the different things one can do with in-built java tools.
I hope you will find it fun too, please share your comment.

22 Aug 2012

Problem of images in a jar file


I was creating a small application using Swing API  in which i had to use some images for various
labels. Then i had to bundle this application in a jar file. The created jar file should display all of the images on execution.


Code for creating a jar file
cmd>jar cmf manifest.txt gui.jar abc/GuiClinet.class images/dice.gif 

Structure of the application :

I created and bundled the application but things didn't turn out as expected. The jar file on execution was not displaying the images.

There is how i add images to labels


/*
                       ImageIcon img = new ImageIcon("images/dice.gif");  
                       JLabel imglab = new JLabel(img);  
                        panel.add(imglab);         
                        frame.add(panel);           
*/




Lets figure out what i was doing wrong and how i corrected it.

1. Use getResource() to get image reference.
Problem : on executing the jar file it was not showing the images.
Solution :The reason for problem was the jar file was not able to get the references of the images. Using the ImageIcon() to get the image reference is not a way to go when looking for image inside a jar file.
So i changed the code  to
/*
String path = "images/dice.gif";
URL  imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);

else {
System.err.println("Couldn't find file: " + path);
return null;
}
*/




2 Directory structure of images inside jar is important.
Problem : After i changed the code and create a new jar file , while creting the jar file i got the error                   Couldn't find fiel images/dice.gif
The reason for the problem i was giving the relative name "images/dice.gif" to the directory "abc" (which is a pacakge  to store .class file) .So the compiler was looking for image files in home_dir/myapp/images/*gif , but since images is not placed under abc directory  thats why i was getting the error
Solution :Placed the /images directory under /abc ,so compiler can now find images in home_dir/myapp/images/dice.gif.
now application structure look something like this





And issued new jar file creation command :
cmd>jar cmf  manifest.txt  gui.jar abc/GuiClient.class abc/images/dice.gif 



Now problem is solved and everybody is happy.

13 Aug 2012

Setting the class path


A classpath is a path required by JVM to look for .class and other resources. Basically by setting the classpath you are telling the JVM  " hey , look for .class files
and other resouces like .jar or .zip files in this directory(s) ONLY". You can set more than one directory for a classpath. A default entry for a classpath is current directory which is represented by a period (.). .

I have Employee.class file in E:\Codes\ directory (Windows 7) . I'm going to set classpath to this directory and run the Employee.class file. I'll use this as an example in all of the methods described below.

Method #1:  jdk_tool -classpath path1;
For Windows, Linux and Solaris 
The jdktool are command line tools such as java , javac, javadoc or apt. One can use -classpath. or -cp (abbreviation form).
Open a commad prompt and issue the following command
>java -classpath E:\Codes Employee
This will tell the JVM to "set the classpath to directory to E:\Codes and run Employee.class file".
Note that if the Employee class would have been under a package, say org,  then fully qualified name of class must be specified.
>java -cp E:\Codes org.Employee

Points to remember
  • There must be a space after -classpath or -cp
  • Multiple path enries must be separated by semicolon (;)
  • This method will overrides the class-path entries defined by CLASSPATH and default value(which is a current directory).



CLASSPATH is a Envionment variable which can be set to a classpath name. There are two methods to set this variable.You can either use set CLASSPATH=path1; command or  System utitliy in Control Panel ( it depends on the operating system).


Method #2:  setting  CLASSPATH variable 
For Windows systems 
One can set the classpath explicity by issuing set CLASSPATH=path1;path2; command at the command prompt. Here the steps for that.
1.open command prompt .
2. type  >set CLASSPATH=E:\Codes
3. run the .Employee.class file using >java Employee.
note that once you closed the command prompt , the classpath gets cleared itself. Next time you have to set it explicitly 
You can unclear the classpath in case if classpath is not set correctly by using : 
>set CLASSPATH=
Points to remember
  • Multiple path entries must be separated by semicolon (;).
  • There should be no space around =.
  • The order in which these path are specified is important since JVM will search in that particular order.
  • The path must either be a file name (.class or .jar or .zip) or directry(which have the .class file(s)).
  • The value of the CLASSPATH environment variable, which overrides the default value (current directory). So if you want the current directory to be searched then you should use "."( period) .
For Linux and Solaris 
To set the classpath in csh shell , use following command
          >setenv CLASSPATH=path:
To set the classpath in csh shell , use following command 
                  >CLASSPATH=path:
                  >export  CLASSPATH

To unclear the classpath in csh shell , use following command
       >unsetenv CLASSPATH
To unclear the classpath in csh shell , use following command 
             >unset CLASSPATH


Method #3: System utility in Control Panel
For Windows XP
       control panel-->System-->Advanced tab
For Windows 7
control panel-->System--> Advanced system setting





For Linux and Solaris 
In csh shell , examine the .cshrc file for setenv command.
In sh shell ,  examine the .profile file for export command.


So many methods to set the classpath , which should one use ?
Method 2 is a recommended way to set the classpath. Because each application can have its own set of  classes to work without  interfering  with any other application.