Compilation error with external Jar - java

I am trying to command line compile my code and I cannot get it to run properly. My project was in eclipse and I could specify the location of a jar file to include but the command line is another issue. All .java and the .jar file are in the same director.
I can compile with
javac -classpath opencsv-2.3.jar *.java
I get the following exception if I try to run my code.
java ClassificationGenerator
Exception in thread "main" java.lang.NoClassDefFoundError: au/com/bytecode/opencsv/CSVReader
I tried running the code with
java -classpath opencsv-2.3.jar ClassificationGenerator
but then I get
Exception in thread "main" java.lang.NoClassDefFoundError: ClassificationGenerator
Caused by: java.lang.ClassNotFoundException: ClassificationGenerator
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

You need to give the jar in the classpath while running, aw well:
java -classpath .;opencsv-2.3.jar ClassificationGenerator

Related

Try to compile a java program that uses jsoup package on Mac

I am trying to compile a Java program that uses jSoup.
Name of Program: WebCrawler.java
I was having issues with getting the program to compile. Thanks to Arvind(user below) that works. However when I java WebCrawler. I get the following errors:
Exception in thread "main" java.lang.NoClassDefFoundError: WebCrawler/java
Caused by: java.lang.ClassNotFoundException: WebCrawler.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Is that because of my classpath?
To compile (on linux):
export CLASSPATH=jsoup-1.8.1.jar
javac WebCrawler.java
or
javac -cp jsoup-1.8.1.jar WebCrawler.java

Getting java.lang.ClassNotFoundException

I am able to compile code but while running it is throughing classnotfound
C:\Users\Desktop\New>javac -classpath java-mail-1.4.4.jar SendMail.java
C:\Users\Desktop\New>java -classpath java-mail-1.4.4.jar SendMail
Exception in thread "main" java.lang.NoClassDefFoundError: SendMail
Caused by: java.lang.ClassNotFoundException: SendMail
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
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: SendMail. Program will exit.
Can you suggest how to solve this?
Try using below commands
To compile java class
javac -classpath .;<jar file path>; Abc.java
To execute java class
java -classpath .;<jar file path>; Abc
Try using:
Windows
java -classpath java-mail-1.4.4.jar;. SendMail
note this ----^

Not Able to Compile Java Code via Command-line

I wrote some Java code that takes several arguments using Eclipse. I was able to compile and execute the code just fine by entering the arguments in the "Run configurations."
However, I need to be able to run my code via command-line. This is what I tried:
javac -g ./MyCode.java
java MyCode ./fileOne.txt ./fileTwo.txt ./fileThree.txt
And the error message:
Exception in thread "main" java.lang.NoClassDefFoundError: mypackage/MyCode
Caused by: java.lang.ClassNotFoundException: mypackage.MyCode
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I don't understand what is going on. My code does not have any bugs when I run through Eclipse. Help?
You are not setting up the classpath properly.
http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

Cannot find CLASS in Terminal

I have downloaded an application that is Java. But I have troubles with running the script.
I have tried "java Main" which properly should run "Main.java"?
But i keep getting this
Exception in thread "main" java.lang.NoClassDefFoundError: Main
Here is my terminal try:
Last login: Thu Jun 13 17:22:39 on ttys001
new-host:~ jespermadsen$ cd /Users/jespermadsen/Desktop/TimeTakerv2-master
new-host:TimeTakerv2-master jespermadsen$ ls
ChallengeHandler.java SBUtil.java
ConnectToSock.java SendToSock.java
CountTime.java Server part
DownloadFiles.java SocketAction.java
Login.java StrangeBase.java
LoginUI.form TimeTakerRenewedUI.form
LoginUI.java TimeTakerRenewedUI.java
Main.java TingGrabber.java
Protocol.java decodeChallengeString.java
QStringBuffer.java jTokenizer.java
RecieveFromSock.java loginToServer.java
ReplaceHTMLChars.java n7
RequestSites.java
new-host:TimeTakerv2-master jespermadsen$ java Main
Exception in thread "main" java.lang.NoClassDefFoundError: Main
Caused by: java.lang.ClassNotFoundException: Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
You need to compile the project. Source files can't be executed.
You need to compile the code first, using javac.
$ javac path/to/Main.java
Then,
$ java path/to/Main

Java: Exception in thread "main" java.lang.NoClassDefFoundError

To compile my java app, I use from terminal:
javac -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar CollectionIndexer.java
To run the app I use:
java -classpath commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar CollectionIndexer
However I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: commons-digester-2/1/commons-digester-2/1/jar:lucene-core-3/0/3/jar
Caused by: java.lang.ClassNotFoundException: commons-digester-2.1.commons-digester-2.1.jar:lucene-core-3.0.3.jar
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
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)
I guess I'm defining correctly the classpath.. or what ?
Try -cp instead of classpath. Also add the current directory to the classpath.
$ java -cp .:commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar CollectionIndexer
or
$ export CLASSPATH=.:commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar
$ java CollectionIndexer
If on Windows, use backslashes and a semi-colon separator:
java -cp .;commons-digester-2.1\commons-digester-2.1.jar;lucene-core-3.0.3.jar CollectionIndexer
I would suggest you to set the classpath first with
SET CLASSPATH command and then execute your program as
java CollectionIndexer.

Categories