Google

Saturday, February 14, 2009

Another Jar example

//SayHello.java
public class SayHello{
public void greet(){
System.out.println("Hi");
}
}



//SayBye.java
public class SayBye{
public void greet(){
System.out.println("Bye");
}
}



//SayTest
public class SayTest{

public static void main(String[] args){
SayHello sh = new SayHello();
SayBye sb = new SayBye();
sh.greet();
sb.greet();
}
}


Assuming all are in same directory and you are also
in the same directory.
Build them:

javac SayHello.java
javac SayBye.java
javac SayTest.java

Now, jar them. Still in the same directory.

jar -cf MyApp.jar *.class

Now, run it.

java -cp .;MyApp.jar SayTest

Note all these this will fail:

java -cp .;MyApp.jar MyApp
java -cp ./MyApp.jar SayTest
java -cp ./MyApp.jar MyApp


The only correct way:


java -cp .;MyApp.jar SayTest

or

java -classpath .;MyApp.jar SayTest