Monday, January 30, 2006

Path & ClassPath

PATH is used by the operating system to find executable files (.exe's in Windows,java,javac,javap) that it can run.

CLASSPATH, on the other hand is specifically used by Java to find .class files that are needed when you run a Java program.


How to execute your first java program.
PATH variable:

Setting the PATH variable allows you "to conveniently run the JDK executables (javac.exe, java.exe, javadoc.exe, etc.) from any directory without having to type the full path of the command."

For example to execute a java program from your “E:\myJavaPrograms\” directory if you didn’t set your PATH variable then you should execute your programs like

E:\myJavaPrograms> c:\jdk\bin\javac MyFirstProgram.java
But if you had your PATH variable then can execute your programs like
E:\myJavaPrograms> javac MyFirstProgram.java

NOTE:
All these executables(javac.exe, java.exe, javadoc.exe, etc.) are in “jdk\bin” folder. So all you have to do is just include this folder path in your PATH variable

In Command Prompt.
set path=”%path%;c:\jdk\bin”
CLASSPATH variable:

Setting the CLASSPATH variable "tells JDK tools and applications where to find third-party (all jar files, zip files that contain .class files) and user-defined classes."

We always include “.” operator in CLASSPATH because that helps the jdk tools to pick up the class files from current working directory(folder)

In Command Prompt.
set classpath=”%classpath%;.;d:\tomcat\servlet-api.jar”

Setting PATH and CLASSPATH in Environment Variables:

Right click on "My Computer", "Advanced" tab, "Environment Variables". We can see two sections
1. User Variables and
2. System Variables

Click New in User Variables

set variable name as JAVA_HOME
set variable value as C:\bea_81\jdk142_05

set variable name as PATH
set variable value as C:\apache-ant-1.7.1\bin;%JAVA_HOME%\bin;.;

% % is used to include the contents in Java_Home

NOTE:
we include %path% and %classpath% so that any previously set path and classpath are also included along with this one.

For further assistance you can look into these links

Setting PATH Variable
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html

Setting CLASSPATH Variable
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/classpath.html
http://faq.javaranch.com/view?HowToSetTheClasspath

Learn about java tools
http://java.sun.com/j2se/1.5.0/docs/tooldocs/index.html


Tomcat:

Modify the startup.bat file in tomcat's bin directory as follows:

set

JAVA_HOME=C:\Program Files\Java\jdk1.6.0_10

for Java_Home - Please point to the jdk folder and not to the bin folder.

set CATALINA_HOME=C:\TOMCAT6

set JRE_HOME=C:\Program Files\Java\jre1.6.0

Compiling and Running java packages

Source File - C:\src\vis\Test.java


C:\src\> javac vis\Test.java
Compile the program from its root directory. That is outside the package structure.

package vis;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}


Run the application like this.
C:\src\>java vis.Test

-d directory
Set the destination directory for class files. The directory must already exist; javac will not create it. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d C:\myclasses and the class is called com.mypackage.MyClass, then the class file is called C:\myclasses\com\mypackage\MyClass.class.


If -d is not specified, javac puts each class files in the same directory as the source file from which it was generated.
Note: The directory specified by -d is not automatically added to your user class path.

Create folder “class” inside src folder.
Now we have (1) vis and (2) class folders inside “src” folder.

-d <destination> <source>

C:\src\>javac -d class vis\Test.java
C:\src\>javac -sourcepath vis -d class vis\Test.java

Executing the program

-cp <destination> <source>

C:\src\>java -cp class vis.Test

Friday, January 27, 2006

Creating a Batch File

Open a text file and type the following
java ProgramName
store this txt file with .bat extension.

Now whenever you click this batch(.bat) file the particular program gets executed.

Here is a tutorial to compile a bunch of source files using a batch file and place them in a proper directory structure.

Creating An Executable JAR File

A little intro about Executable JAR File

An executable .jar file is simply a .jar file with an extra line in the manifest:

Main-Class: MyPackageName.ClassName

When you double click on this .jar file, the applciation should launch(*)

However, an executable .jar file IGNORES the system CLASSPATH variable. You can specify a Class path in the manifest:

Class-Path: anotherJarFile.jar ..

Note the capitalization and hyphenation of "Class-Path" Also note that the elements listed are relative to the location of the .jar file and not relative to the current directory or anything else. Also note that the different elements are separated by spaces, not : or ;
---------------
(*) This is, of course, assuming that the JRE is correctly installed. Just include the distribution .exe with the application, and tell the client that if they don't have java already installed on the target machine, they will have to install it before running their program. I usually have the client open up a DOS prompt (or the equivelent) and type java -version. This double checks that java is installed and enables you/the user to verify that the required version is installed. There are ways to automate this for "dumb" users, but I gave my mother these instructions and she could follow them....

A wonderful tutorial that explains about
Executable JAR File.

In the first example author has used a Swing Frame which opens a Frame Window. If you had NOT matched the file type then when you click the jar file zip application will open.

Our aim is to display the frame window when user clicks the jar file. To do this we got to set the file type to javaw

of "D:\Program Files\Java\j2re1.4.2_03\bin\javaw.exe"

How to do this?

Open Windows Explorer, tools\folder options\File Types tab and scroll down to jar file click "change" button "open with" dialogue box appears click "others..." button and select
D:\Program Files\Java\j2re1.4.2_03\bin\javaw.exe". give open and OK.

After completing the above procedure when you click the jar file you will see the "Frame Window".

NOTE:
An executable jar file is defaulted to run with the javaw command, not the java command. This means that you won't get a DOS prompt/whatever upon double clicking, and any System.out.prinln() messages (or the equivelent) will not show up. This includes even when the program is designed to take input from user at command prompt.

exe4j

exe4j is a Java exe maker that helps you integrate your Java applications into the Windows operating environment

Friday, January 20, 2006

Difference between Class.forName() and ClassLoader.loadClass()?

How to find the NAME of your Java file.


class MyJavaProgram {
public static void main(String[] args) {
System.out.println("Class Name: " + Testing.class.toString().substring(6));
}
}


Output:
Class Name: MyJavaProgram

NOTE:
System.out.println("Class Name: " + Testing.class);
Class Name: class MyJavaProgram

Friday, January 13, 2006

Difference between exe file and batch file
Differnce between scripting langusge(javascript) and compiler based lang (java)
About XML
Memory allocations
Under the Hood JVM Articles from Sun

You can learn about all these things in this thread

Thursday, January 05, 2006

Difference between Object and Reference