1

I have a jar file which includes the manifest entry to the main class and I was trying to run the jar using the -jar option and then came to know that using the -jar option ignores the classpath setting.

Previous Usage: java -jar MyJar.jar

Now I have all my rest of the jars configured in $CLASSPATH.

How do I include the same while running my jar.

BTW I'm running on a Linux OS.

david.colais
  • 599
  • 1
  • 6
  • 16
  • In fact sun/oracle have been trying to simplify this for last 11 years: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4648386 – Anupam Saini Dec 02 '13 at 05:03
  • Do any of your classes include a package declaration? This is important! – diegoaguilar Dec 02 '13 at 05:45
  • @diegoaguilar : Yes my classes are housed in a package and in my manifest.txt file the main class attribute is defined as follows `Main:Class: MyJar.HelloWorld` – david.colais Dec 02 '13 at 05:48

2 Answers2

1

java -cp $CLASSPATH -jar MyJar.jar

EJK
  • 11,784
  • 3
  • 34
  • 53
  • It gives me an error saying `Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Message` but I see that the mail.jar is present in my $CLASSPATH. Is there anything I need to modify to refer the dependent jars. – david.colais Dec 02 '13 at 05:02
  • are you 100% sure that all needed jars are in the $CLASSPATH directory? If on Windows, beware of "\" as a path separator. Try setting CLASSPATH with forward slashes, e.g. CLASSPATH=c:/foo;c:/bar – EJK Dec 02 '13 at 05:05
  • Yes all jar files are on the CLASSPATH variable and a part of the CLASSPATH is as follows `:/dirjar/adv/lib/log4j.jar:/dirjar/adv/lib/mail.jar:`.What is wrong here. BTW as I mentioned it is Linux OS. – david.colais Dec 02 '13 at 05:10
  • If you insist on setting the classpath the way you described in your question, here is the workaround (It's valid only for jdk6 and above) - http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath – Anupam Saini Dec 02 '13 at 05:19
  • The version of java in my server is `1.5.0_06` is there any other way to solve my issue. – david.colais Dec 02 '13 at 05:33
0

The easy approach (provided you are familiar with ant scripts) would be to create a build script to generate an executable jar. You can define a compile rule, a rule to create an executable jar that has other jars (in exploded format) contained within it.

Please note that there is no standard way to include a jar within a jar. Classpath including JAR within a JAR

I have already done something similar here you might want to have a look the ant build file

Community
  • 1
  • 1
Anupam Saini
  • 2,321
  • 2
  • 21
  • 29