简体   繁体   English

运行应用程序时,如何传递Java命令执行的附加jar?

[英]How do I pass an additional jar do my java command when running application?

I have a java gradle projectA that references another java gradle projectB, that builds as a lib. 我有一个Java gradle projectA,它引用了另一个以lib构建的java gradle projectB。

My gradle build configuration seems to be fine, since I can import and use classes from the other project, and it compiles. 我的gradle构建配置似乎很好,因为我可以导入和使用其他项目中的类,并且可以对其进行编译。 But when I try to run the application from the command line I get an error... 但是,当我尝试从命令行运行应用程序时,出现错误...

Exception in thread "main" java.lang.NoClassDefFoundError: dawcore/SamplerInstrument at DawCLI.main(DawCLI.java:17) Caused by: java.lang.ClassNotFoundException: dawcore.SamplerInstrument at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more 线程“主”中的异常java.lang.NoClassDefFoundError:DawCLI.main(DawCLI.java:17)处的dawcore / SamplerInstrument导致原因:java.net.URLClassLoader.findClass(URLClassLoader.java处的java.lang.ClassNotFoundException:dawcore.SamplerInstrument。 :382)在java.lang.ClassLoader.loadClass(ClassLoader.java:424)在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:349)在java.lang.ClassLoader.loadClass(ClassLoader.java:357) ...还有1个

It seems to be complaining that it can't load the classes in the jar at runtime....which makes sense. 似乎在抱怨它无法在运行时加载jar中的类。 But I don't know how to have it successfully load those classes. 但是我不知道如何让它成功加载那些类。

my current run command that does not reference the jar, is as follows.... 我当前不引用jar的运行命令如下。

/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java -ea -cp "build/classes/main/" DawCLI

Running this gives the initially mentioned error. 运行此命令将产生最初提到的错误。

I then read the docs on java -cp argument. 然后,我阅读有关java -cp参数的文档。 It says to provide additional classpath directories, to separate them by semicolon. 它说提供额外的类路径目录,用分号分隔它们。

I susequently updated my run command to be as follows.... 我经常将运行命令更新如下。

/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java -ea -cp "build/classes/main/;../DawCore/build/libs/DawCore.jar" DawCLI

This gives me the following error.... 这给了我以下错误。

Error: Could not find or load main class DawCLI 错误:找不到或加载主类DawCLI

My main function is as follows.. 我的主要功能如下。

import dawcore.*;

public class DawCLI {
    public static void main (String [] args) throws IOException {
        SamplerInstrument samplerkick = new SamplerInstrument();
    }
}

According to the docs I seem to be doing this correctly, but am still getting errors. 根据文档,我似乎正确地做到了这一点,但是仍然出现错误。 Any insight on this would be greatly appreciated! 任何对此的见解将不胜感激!

Thanks 谢谢

The solution was to replace the delimiting semi-colon with a colon instead. 解决方法是用冒号代替定界分号。 Even though the first line of the javadocs on -cp command indicate that you should use a semi-colon. 即使javadocs on -cp命令的第一行指示您应使用分号。

This could be because I am running on linux and those docs indicate Windows. 这可能是因为我在Linux上运行,并且这些文档指示Windows。

https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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