Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

terminal

jar

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 ?

like image 481
CanCeylan Avatar asked Feb 08 '12 19:02

CanCeylan


People also ask

How do I run a JAR file from an external jar?

To include external JAR files, you can either: Copy all the JAR files of the external packages to the Java's Extension Directories (NOT applicable to JDK 9). For Windows, the JDK extension directory is located at " <JAVA_HOME>\jre\lib\ext " (e.g., " c:\Program Files\Java\jdk1. 8.0_xx\jre\lib\ext ").


1 Answers

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.

like image 193
user unknown Avatar answered Sep 22 '22 06:09

user unknown