Re: Running java programs from class files



Knute Johnson wrote:

So now that you solved that one, show me how to use a jar library on the command line when I run a java program. I can make it compile but I can't make it run. I can get it to work if I put the jar file in the Class-Path: line in the manifest and put the jar file in the same directory as the program jar file but not from the command line.

Test is my slightly modified class that calls a static method in lib.Lib. The Lib class has been compiled and put into a jar file, lib.jar in the /lib directory. I then compile Test.java from the /test directory and run the .class file with no problems. You will see that the lib.Lib class can't be found once I jar it.


I don't think it is possible to achieve that if you're using -jar option of java. Once the -jar option is specified, whatever you specify as a -classpath or -cp option is essentially ignored, and the effective user classpath of the JVM becomes the specified jar file and the jars specified in that jar's manifest.

An alternative would be to not use the -jar option, but to run it as :

java -cp /path_to_lib_jar/lib.jar:/path_to_test_jar/test.jar test.Test

(i.e by explicitly specifying the main class to invoke, and specifying all the jar files in the classpath option)

BK
.



Relevant Pages

  • Re: Running java programs from class files
    ... the command line when I run a java program. ... I can get it to work if I put the jar file in the Class-Path: line in the manifest and put the jar file in the same directory as the program jar file but not from the command line. ... I then compile Test.java from the /test directory and run the .class file with no problems. ... (i.e by explicitly specifying the main class to invoke, and specifying all the jar files in the classpath option) ...
    (comp.lang.java.programmer)
  • Re: Creating a .jar package file
    ... > Java class names should be meaningful, ... I wrote Prob3Customer. ... that is why I'm trying to get the .jar file somewhere so I can ... compile my application Prob3PaneApp. ...
    (comp.lang.java.help)
  • Re: Jar file only works on my pc
    ... I use textPad 4 to compile and command line to make ... I open the jar file using 7zip and all of the class files are ... my other applets work fine. ... I think I have the $'s because of my actionListener inner classes. ...
    (comp.lang.java.programmer)
  • Re: Eclipse IDE Question
    ... and debug but can't figure out to compile a JAR file! ... Eclipse compiles Java files on ... What do you exactly mean by compiling a JAR file? ... Thanks Roland - I didn't know about the "Export" step. ...
    (comp.lang.java.programmer)
  • Re: Unable to execute program using the jar option.
    ... by specifying the class directly it runs just ... Exception: I believe the exact Exception is irrelevant to this issue ... path definately has the jar file that contains the class the exception ... complaining about finding the MigrationDirector .. ...
    (comp.lang.java.programmer)