How to compile java project with external jar file in Linux terminal

妖精的绣舞 提交于 2019-12-09 12:33:35

问题


I have project which includes external jar file in it, I followed this link http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29 to add external java path. Then I tried to compile my code in terminal, however I am still get an error about jar file does not exist.

I wrote the following commands: (Currently I am in the project directory and there are three folders called bin src and lib in there)

bash-3.2$ ls
bin  lib  README.txt  src
bash-3.2$ javac -cp lib/jsoup-1.6.1.jar src/DayTradingStockBlog.java
bash-3.2$ java -cp .:lib/jsoup-1.6.1.jar src/DayTradingStockBlog
Exception in thread "main" java.lang.NoClassDefFoundError: src/DayTradingStockBlog (wrong name: DayTradingStockBlog)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: src/DayTradingStockBlog.  Program will exit.

How should I solve this problem ?


回答1:


You never use slashes, which are path delimiters, in a call to java (but to javac). If src is part of your package declaration - in this case the whole package declaration, which I bet it is not, you would, instead of:

 java -cp .:lib/jsoup-1.6.1.jar src/DayTradingStockBlog

use a dot:

 java -cp .:lib/jsoup-1.6.1.jar src.DayTradingStockBlog

But I guess it is just the place where you created the class, so the path belongs to the classpath:

 java -cp .:lib/jsoup-1.6.1.jar:./src DayTradingStockBlog

You aren't free to omit the path from the Class name, and append it to the classpath, or vice versa - it has to fit to your package declaration.

If you declare a package foo, (which has much more sense than src), your class name is no longer DayTradingStockBlog but foo.DayTradingStockBlog.




回答2:


Based on your edit, I'd suggest you to enclose all classpath locations within single/double quotes. That is, make it like

java -cp '.:lib/jsoup-1.6.1.jar' src/myClass.

In linux, the items in the classpath are separated by a colon (:) and in Windows, it's a semicolon (;).




回答3:


use classpath (-cp)

javac -cp %YOUR_JAR_LOCATION% myClass.java 



回答4:


you should place the java file and jar file in the same dir example: javac -cp jdbc.jar myClass.java it works for me



来源:https://stackoverflow.com/questions/9200334/how-to-compile-java-project-with-external-jar-file-in-linux-terminal

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