10/10/2012

Java compile/run files that need external jar files

If developer wants to compile the java files with external jar files.

For example. Compile EmailTest.java with jar files: mail.jar. Put the jar file under the same directory with the java file, and execute in the cmd line:

javac -cp mail.jar EmailTest.java

Then we can see that the class file EmailTest.class is generated.

Then execute the java code:

Now I have a problem. I have tried 
java -classpath mail.jar EmailTest

But it does not work. I have to put the mail.jar to the jre/lib/ext, and then execute 

java EmailTest

Then it works.

I'll keep looking for other ways.

Except for that I read from other post. We can wrap the jar file with the code into new jar file and execute that jar file. Then it'll work fine.

java -jar XXX.jar


No comments:

Post a Comment