3

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?

2
  • Can you show your jar file's manifest file? Does it include a class-path statement directed to the guava jar files? Commented Aug 8, 2012 at 16:58
  • Did you tried with "java MyScanner -cp guava-13.0.jar" ? Commented Aug 8, 2012 at 16:58

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much :-) That was exactly what I needed to run my program. Now I don't get more ClassNotFound Problems
No problem. Typically on SO - we accept an answer that helped us solve the problem. You can do so by clicking the check box next to the answer you felt addressed your issue best.
1

You should try the below thing

java -classpath myJar.jar my.package.Program

Comments

1

Because anything after your classes name

java MyScanner -whatever is --after
               ^         ^  ^ 

will be arguments to the main method in your class and everything before your class name

java --what -is -before MyScanner
     ^      ^   ^

will be arguments for the jvm. -cp or -classpath must be a jvm argument not an argument for your program.

It must read

java -cp myPackages/guava-13.0.jar MyScanner 

1 Comment

If I try this I get: Exception in thread "main" java.lang.NoClassDefFoundError: MyScanner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.