Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a java program with external jar

Tags:

java

jar

external

I want to run my program with guava. If I compile my program with

EDIT: java -> javac for the compile call

javac -cp myPackages/guava-13.0.jar MyScanner.java    

there is no problem.

If I try to run

java MyScanner -cp myPackages/guava-13.0.jar 

I get this output on the console:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Optional
at MyScanner.main(MyScanner.java:37)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Optional
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)
... 1 more

Can you tell me how I can execute the program with the external jar?

like image 797
lennykey Avatar asked Jul 18 '26 15:07

lennykey


2 Answers

I think what you want is:

java -cp myPackages/guava-13.0.jar:. MyScanner

Notice I'm setting two values in the classpath, '.' (current directory) and the path to guava. Your problem is that you specified the classpath option after you specified your main class MyScanner. Options that are specified after your main class are arguments to your program, not to java itself.

like image 184
Amir Afghani Avatar answered Jul 20 '26 05:07

Amir Afghani


You should try the below thing

java -classpath myJar.jar my.package.Program
like image 42
Bhavik Ambani Avatar answered Jul 20 '26 03:07

Bhavik Ambani