-2

I have a newbie question on java; have been unable to set classpath properly. Am missing something very basic and trivial.

I have a jar file. foo.jar, in e:\bar.

This works from command line, when the current directory is e:\bar

java -jar foo.jar <params> 

When I change directory to e:, it starts throwing class not found exceptions. So I set -cp to specify the class path. Whatever I have tried so far, it has failed to work

java -cp ".;e:/bar/*;e:/bar/lib/*" -jar bar/foo.jar <params>
java -cp "." -jar bar/foo.jar <params>
....

I always get this exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/httpclient/MultiThreadedHttpConnectionManager
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.apache.axis.utils.ClassUtils$2.run(ClassUtils.java:187)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.axis.utils.ClassUtils.loadClass(ClassUtils.java:171)
        at org.apache.axis.utils.ClassUtils.forName(ClassUtils.java:112)
        at org.apache.axis.deployment.wsdd.WSDDTargetedChain.makeNewInstance(WSDDTargetedChain.java:196)
        at org.apache.axis.deployment.wsdd.WSDDDeployableItem.getNewInstance(WSDDDeployableItem.java:310)
        at org.apache.axis.deployment.wsdd.WSDDDeployableItem.getInstance(WSDDDeployableItem.java:296)
        at org.apache.axis.deployment.wsdd.WSDDDeployment.getTransport(WSDDDeployment.java:470)
        at org.apache.axis.configuration.FileProvider.getTransport(FileProvider.java:282)
        at org.apache.axis.AxisEngine.getTransport(AxisEngine.java:283)
        at org.apache.axis.client.AxisClient.invoke(AxisClient.java:179)
        at org.apache.axis.client.Call.invokeEngine(Call.java:2564)
        at org.apache.axis.client.Call.invoke(Call.java:2553)
        at com.ideas.jws.gftservice.GFTMessenger.sendMessage(GFTMessenger.java:1
Amit
  • 1,836
  • 15
  • 24
  • Since when windows started using '/' as directory separator instead of '\'? – zubergu Jul 22 '15 at 07:23
  • @zubergu Either one has worked since about 15 years ago (if not more). – user253751 Jul 22 '15 at 07:24
  • I would recommend you use eclipse to export a Java project combined with the dependency JAR files, then you don't need to set classpath at all , Refer this http://stackoverflow.com/questions/11033603/how-to-create-a-jar-with-external-libraries-included-in-eclipse – Dickens A S Jul 22 '15 at 07:42
  • yeah; you would recommend someone to download this, download that; do this, all to run a program? Seriously? – Amit Jul 22 '15 at 07:43
  • At least you try putting the specific JAR file names instead of wild card *, I assume the apache-commons-httpclient is somehow missing its dependencies when you specify * – Dickens A S Jul 22 '15 at 07:44
  • Thank you. I stuck with launching the jar from the directory where the jar resided. Trying to launch it from some other directory was proving to be too much of a bother. – Amit Jul 22 '15 at 12:00

2 Answers2

0

-cp does not work with -jar.

If you know the main class name, then you can work around it by not using -jar:

java -cp ".;e:/bar/*;e:/bar/lib/*;bar/foo.jar" main.class.name.Here <params>
user253751
  • 57,427
  • 7
  • 48
  • 90
0

As the application jar is dependent of a specific set of jars, one common practice is to have:

distribution
 ├─ myapp.jar
 └─ lib
     ├─ libaaa-1.2.3.jar
     └─ libbbb-2-3-4.jar

In META-INF/MANIFEST.MF of the application jar one often generates a Class-Path: ... entry.

Then no you can run just the application foo.jar itself.

To automate such dependencies, my personal preference is using maven. (gradle is popular too.)

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • The foo,jar, that I got from a third party, is in this format. foo.jar and then a lib folder. There is no META-INF file though. – Amit Jul 22 '15 at 07:37
  • I was hoping that it is something trivial that I am missing. Surely I do not require to jump through hoops to run a basic java program .... – Amit Jul 22 '15 at 07:38