简体   繁体   中英

unable to run java class from command prompt refering to a jar in classpath

I am trying execute a java class which accesses a method in a jar file.My package structure is com.xmlpost.XmlPostInit.java and it resides under the directory:

  D:\Workspace\Test\bin

I am using the command below. Note, cs.jar contains the class com.xmlpost.XmlPostInit

D:\Workspace\Test\bin>java -classpath D:\FatWire\JSK\7.6.0\App_Server\apache-tomcat-6.0.18\webapps\cs\WEB-INF\lib\cs.jar com.xmlpost.XmlPostInit

.. which gives me following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/xmlpost/XmlPostInit
Caused by: java.lang.ClassNotFoundException: com.xmlpost.XmlPostInit
        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)
Could not find the main class: com.xmlpost.XmlPostInit.  Program will exit.

You have to add your bin directory to the classpath too, as the JVM needs to look inside it to find your class. As you are running inside it, you can refer localy it with ".":

D:\Workspace\Test\bin>java -classpath .;D:\FatWire\JSK\7.6.0\App_Server\apache-tomcat-6.0.18\webapps\cs\WEB-INF\lib\cs.jar com.xmlpost.XmlPostInit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM