通过命令提示符从JAR运行具有多个JAR依赖项的类

时间:2015-05-27 15:34:44

标签: java jar cmd openjpa

我无法理解为什么这些都不起作用。我在stackoverflow上看到过类似的情况,他们的解决方案对我没用。

我有一个包含15个JAR的lib目录。当我在命令提示符下从该lib目录中运行此命令时:

java -cp openjpa-2.2.1.jar org.apache.openjpa.enhance.PCEnhancer

我为其中一个依赖项获得NoClassDefFoundError

Exception in thread "main" java.lang.NoClassDefFoundError: serp/bytecode/Instruction
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Caused by: java.lang.ClassNotFoundException: serp.bytecode.Instruction
        at java.net.URLClassLoader$1.run(Unknown Source)
        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)
        ... 7 more

现在,当我尝试添加包含该依赖项的jar时:

java -cp openjpa-2.2.1.jar:serp-1.13.1.jar org.apache.openjpa.enhance.PCEnhancer

我明白了:

  

错误:无法找到或加载主类org.apache.openjpa.enhance.PCEnhancer

我也试过了:

java -cp openjpa-2.2.1.jar:serp-1.13.1.jar:. org.apache.openjpa.enhance.PCEnhancer

java -cp . org.apache.openjpa.enhance.PCEnhancer

具有相同的效果,当我尝试时:

java -cp serp-1.13.1.jar -jar openjpa-2.2.1.jar org.apache.openjpa.enhance.PCEnhancer [path to .java file]

我得到的只是:

>OpenJPA 2.2.1
>version id: openjpa-2.2.1-r422266:1396819
>Apache svn revision: 422266:1396819

>os.name: Windows 7
>os.version: 6.1
>os.arch: amd64

>java.version: 1.8.0_25
>java.vendor: Oracle Corporation

>java.class.path:
>        openjpa-2.2.1.jar

没有文件输出,即使我在目录中添加-d标志。 我哪里错了?

1 个答案:

答案 0 :(得分:0)

RealSkeptic的评论是正确的。分号是分隔Windows中类路径上列出的JAR的正确语法,如下所示:

for test in test_*.txt do 'rename test-name.txt to test-name(number in test).txt' done