run java from terminal that depend on external libraries

老子叫甜甜 提交于 2019-12-11 02:28:49

问题


I've read around a lot but haven't been able to find a solution yet.

I'm using lwjgl, it needs 2 jars and a native library to run: lwjgl.jar, lwjgl_util.jar and the natives library. I've tried this in every way i could think of, anyway, I'm trying with a command like this at the moment:

java - Djava.library.path="libs/natives/" -cp libs/jars/lwjgl.jar:libs/jars/lwjgl_util.jar DisplayTest.class

but in every way i try, i get:

Exception in thread "main" java.lang.NoClassDefFoundError: DisplayTest/class
Caused by: java.lang.ClassNotFoundException: DisplayTest.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: DisplayTest.class. Program will exit

Oh and it might be worth to mention that I'm working on a linux terminal. Also, i get this to run perfectly fine in eclipse so I can't really understand whats up here.


回答1:


First off, you just need to pass the class name to java:

java -Djava.library.path="libs/natives/" 
     -cp libs/jars/lwjgl.jar:libs/jars/lwjgl_util.jar DisplayTest

(linebreaks for readability)

I would try the following:

1) Use the CLASSPATH enviroment variable, as in:

  CLASSPATH=.:/path/to/lwjgl/lwjgl.jar:/path/to/lwjgl/lwjgl_util.jar 
  export CLASSPATH

Notice the dot (.) at the very beginning of the classpath;

2) Run your java application:

  java -Djava.library.path="libs/natives" DisplayTest

If this works, add the commands above to a shell script. Good luck!




回答2:


Try taking off the ".class" e.g.

java -Djava.library.path="libs/natives/" -cp libs/jars/lwjgl.jar:libs/jars/lwjgl_util.jar DisplayTest.class

Would become:

java -Djava.library.path="libs/natives/" -cp libs/jars/lwjgl.jar:libs/jars/lwjgl_util.jar DisplayTest

By adding the ".class", you're telling the command that you have a Class called "class" in a directory called "DisplayTest", which is not what you're trying to achieve. This is shown in this line:

Exception in thread "main" java.lang.NoClassDefFoundError: DisplayTest/class



回答3:


when triggering java command use -cp or -classpath. Enter just java command to see the usage




回答4:


I also got a similar error.

Just include your working directory , (where you have your own class), along with the libraries that you need in your classpath at runtime.

NoCLassDefFoundError occurs when the machine is unable to find your .class files during runtime (even though they have been compiled ,i.e it won't give an error while compiling but only during runtime)

STEP 1: to compile javac -classpath "path/to/lib1:path/to/lib2" yourfile.java STEP 2: to run java - classpath "path/to/lib1:path/to/lib2:path/to/your/currentdirectory/wherethedotclass/filearecreated/" yourfile



来源:https://stackoverflow.com/questions/12267949/run-java-from-terminal-that-depend-on-external-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!