1 Reply Latest reply on Jun 21, 2012 6:34 AM by Wolf-Dieter Fink

    dependent class can't be found when invoke by java -classpath xxx -jar jarfile, but can be found by java -classpath xx class

    Bond Chan Novice

      c1, java -cp LRS3rdPartyClient.jar -jar tasks.jar

      c2, java -classpath LRS3rdPartyClient.jar;tasks.jar com.lombardrisk.build.WaitForServerStart

      c1 can't be executed due to an class(org.jnp.interfaces.NamingContextFactory) can't be found which reside in LRS3rdPartyClient.jar, whereas the c2 can execute

      quite confused,


      exception thrown by c1:

      Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException:


              at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)

              at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)

              at javax.naming.InitialContext.init(Unknown Source)

              at javax.naming.InitialContext.<init>(Unknown Source)

              at com.lombardrisk.build.WaitForServerStart.main(WaitForServerStart.java:29)

      Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory

              at java.net.URLClassLoader$1.run(Unknown Source)

              at java.security.AccessController.doPrivileged(Native Method)

              at java.net.URLClassLoader.findClass(Unknown Source)

              at java.lang.ClassLoader.loadClass(Unknown Source)

              at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

              at java.lang.ClassLoader.loadClass(Unknown Source)

              at java.lang.Class.forName0(Native Method)

              at java.lang.Class.forName(Unknown Source)

              at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)

              ... 5 more

        • 1. Re: dependent class can't be found when invoke by java -classpath xxx -jar jarfile, but can be found by java -classpath xx class
          Wolf-Dieter Fink Master

          This is not really a JBoss queestion, you should read the Java documentation.

          here a snippet from the "java" command

          Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests.

          When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored. 


          If you want to use -jar add the dependencies to other jars to manifest and copy all jars in the same folder.