Thursday, 16 August 2012

Javac and Java Command Lines

javac and java commands are the most basic thing in Java Programming. If you are programming using an IDE, such as Netbeans-like me, this might not be a problem. However, if you use command line and compile-run your Java programs from the shell, then you might need to read on.

To compile a java program (with .java extension), you need to compile it from the same directory where it is located, or you can also specify where it is. For example, here is my PackageDemo.java, A.java and D.java codes:

















I put PackageDemo.java in directory Documents/JavaFiles/sources/packagedemo. In my directories, I separated the source files and the class files. So, all the .java files are in the Documents/JavaFiles/sources folder along with their package names as the subdirectories. All the .class files are in Documents/JavaFiles/classes folder. The way I compile PackageDemo.java is that I specify where I want the compiler to put the .class files by:
javac -d command

By this, the compiler will automatically compile A.java and D.java. It also automatically creates 3 extra folders in my Documents/JavaFiles/classes directory, namely: 
  • 1 for package packagedemo (from PackageDemo.java) that contains PackageDemo.class. This is because we specify it in our program by the statement of "package packagedemo;"
  • 1 for package abc (from A.java) that contains A.class. Again, this is because we specify it in A.java by the statement of "package abc;"
  • 1 for package def (from D.java) that contains D.class. Again, this is because we specify it in D.java by the statement of "package def;"
Folders created automatically by compiler
because of the 'package' statement in each .java code
How to run the PackageDemo.class? I do it from the parents directory that contains the packages, that is from Documents/JavaFiles/classes directory. This make sense because the JVM, just like the Java compiler in this case, looks for .class files by using a class loader that searches, in order, through:
  1. the standard Java classes first that are bundled with the JDK. 
  2. If not found, it goes to extension classes provided by the Java's extension mechanism. 
  3. If still not found, the class loader searches the classpath, which by default is the current directory. 
Here is the command line for running PackageDemo.class and the result:

Java command line
Result of the above command line
You can set up your own classpath if you want. This can be done in 2 ways:
  1. provide -classpath option when compiling to command line javac 
  2. set you CLASSPATH environment variable


Tuesday, 14 August 2012

Where is the jnlp library??

For those who are doing Java programming with Java Web Start or, like me, following through Deitel's Java How to Program chapter 24, you might be wondering where you can find the JNLP library. I did a bit of research and I finally found that the jnlp library located inside javaws.jar.

I am currently using JDK7 on Ubuntu 12.04 and the javaws.jar is located in /usr/lib/jvm/java-7-oracle/jre/lib. Basically it is located in jre/lib folder if you are using other operating systems and have installed JDK with different names.

Having located the exact place of the jar files, I added it as part of the libraries path to my Netbeans 7.1.2 by clicking on the Netbeans menu bar:

Tools > Libraries > New Library...

I put it under the name of JavaWebStart with the Library Type as "Class".

After that, on the same Library Manager window, I look for the JavaWebStart library, click on it and on the right hand side of the window, under Classpath tab, I click "Add JAR/Folder..." A Browse JAR/Folder window opens up. I navigate to where the javaws.jar is located and click Add JAR/Folder.

Having done that, in order to be able to use it in my project, I need to set up the project's library by right-clicking the project's name and click Properties. A Project Properties window opens up. Under Categories, on the left hand side of the window, click Libraries. Then on the right hand side of the window, you will see the Compile tab. All you need to do is click the "Add Library..." button and choose the library we have just added. Click OK to close the window.

That's it!

Well, if you just want to use the library in the project, I suppose you can skip the first part of Adding a new Libarary and just go to the project's properties and choose "Add JAR/Folder" button.

Hope this helps. : )


Welcome

Welcome to my new "All Java!!" blog! I will share all the the things I learn about Java programming on a daily basis here on the site.

I have known Java for a long time. I started my programming skill with Assembly and C. Then I slowly crawled into C++ and C# before finally got onto Java. I love Java the moment I saw it! And now I get to share my learning bits with you. So I do hope you would find this site useful. Enjoy! :)