Issue with running java class file containing Jsoup dependency - java

I am currently having a problem I believe deals with some classpath problems but I haven't found any way to fix it. I currently have the following java code:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JsoupTest {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws IOException {
String url = "http://en.wikipedia.org/wiki/Main_Page";
Document doc = Jsoup.connect(url).get();
String result = doc.text();
System.out.println(result);
}
}
I have the jsoup-1.7.3.jar file in the same directory as the java file(to keep things simple).
I ran the command "javac -cp .:jsoup-1.7.3.jar JsoupTest.java" which compiles and works fine. However when I go and run "java JsoupTest" I get the following error
java JsoupTest
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at JsoupTest.main(JsoupTest.java:31)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
What is the possible error and how exactly would I go about fixing it? I believe it's a classpath issue but the classpath should already be set up when I compiled it? Any help would be appreciated. Thank you.

Just like the compilation command you need to include the jar in the classpath
java -cp .:jsoup-1.7.3.jar JsoupTest
For windows use .; instead of .:
java -cp .;jsoup-1.7.3.jar JsoupTest

Try:
java -cp .:jsoup-1.7.3.jar JsoupTest

Related

Included Jsoup library will compile but not run

I'm trying to explore webscraping in Java and am just starting with a very basic Jsoup program to get started. I am pretty sure there is some sort of path issue with what I am doing. I have tried different variations, and just to get it working have simplified the process by including the library in the same directory as my source file(to simplify the path while I figure out what is going on). Here is what I have been doing and the output:
javac -cp jsoup-1.7.3.jar URLParse.java
The above compiles with no errors(it also compiled fine when I had the jar in its own folder and specified the path), the below happens when I try to run the program:
java -cp jsoup-1.7.3.jar URLParse.java
Exception in thread "main" java.lang.NoClassDefFoundError: URLParse/java
Caused by: java.lang.ClassNotFoundException: URLParse.java
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:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: URLParse.java. Program will exit.
The following is the code in case it help:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class URLParse{
public static void main(String[] args){
String URL = "http://www.google.com";
try{
Document doc = Jsoup.connect(URL).get();
System.out.println("Ok here");
String title = doc.title();
System.out.println(title);
} catch (IOException e){
e.printStackTrace();
}
}
}
Thanks for any help or suggestions.
I think the issue here is that you're not including the directory with the code of your main class in the classpath variable.
java -cp jsoup-1.7.3.jar URLParse.java
Should be
java -cp .:jsoup-1.7.3.jar URLParse
On Mac/Linux and
java -cp .;jsoup-1.7.3.jar URLParse
On Windows. Note that you don't include .java on the class you're trying to run.
Information taken right from this.
Assuming you have a similar directory structure:
and you are trying to run your URLParse class you would need:
java -classpath testPackage/jsoup-1.7.2.jar:. testPackage.URLParse
For more details, please refer Java Glossary

java linux server: class not found exception though jar in classpath

Im running a Java application on an Amazon EC2 instance which is a linux server. I included a json jar in the build path and I checked the class path and it's there. When I import the class
import org.json.JSONException;
import org.json.JSONObject;
I dont get any compilation errors, but when I execute my app I get the ClassNotFoundException.
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: org/json/JSONException
at Group.run(Group.java:62)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
Does anybody know how to fix it?
I have tried to fix this for weeks now, I'm really frustrated!
I think it may be the environment setting problem.
Try to edit ~/.bash_profile and append the CLASSPATH value like:
CLASSPATH=$CLASSPATH:/your/jar/folder/
export CLASSPATH
Then using following cmd to make the setting work:
source ~/.bash_profile

classpath error in java

I have written a java program where I am using a .jar library (Jinterface). I compile it using:
javac -classpath lib/OtpErlang.jar Game.java Actions.java
where the lib dir is inside my project folder.
There is no error when compiling but I get a runtime error like this:
Exception in thread "main" java.lang.NoClassDefFoundError: com/ericsson/otp/erlang/OtpErlangObject
at Game.main(Game.java:7)
Caused by: java.lang.ClassNotFoundException: com.ericsson.otp.erlang.OtpErlangObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
And I think that it has to do with the classpath, what is wrong?
Here is the Game class:
import java.io.IOException;
public class Game {
public static void main(String[] args) throws IOException {
Actions server = new Actions();
server.moveRight();
}
}
Try running it like this, from the root of all your java packages:
java -classpath lib/OtpErlang.jar Game
This command did the trick:
java -classpath lib/OtpErlang.jar:. Game
I have missed the :. (which tells where my main class is, I suppose...)
Try like this.I hope this will help.....
javac -cp . lib/OtpErlang.jar Game.java Actions.java

JAVA Exception in main thread java.lang.NoDefFounderror: net/........./AbstractClock

I have a clock program as AbstractClock.java in C:\Users\Neeraj\Prg folder.
CLASSPATH is specified as C:\Users\Neeraj\Prg
COMPILED javac AbstractClock.java with no errors.
RUN AS java -cp . net.sf.fmj.ejmf.toolkit.media.AbstractClock
Getting errors as
Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/fmj/ejmf/toolkit/media/AbstractClock
Caused by: java.lang.ClassNotFoundException: net/sf/fmj/ejmf/toolkit/media/AbstractClock
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:315)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: com.hib.TranslateSQL. Program will exit.
PROGRAM:-
package net.sf.fmj.ejmf.toolkit.media;
import java.lang.*;
import javax.media.Clock;
import javax.media.Time;
import javax.media.TimeBase;
public class AbstractClock implements Clock {
private TimeBase systemtimebase = Manager.getSystemTimeBase();
public AbstractClock() { super(); }
///REST ALL METHODS
}
I am running this in commandprompt in Windows. I have seen other posts and changed classpath and run command, but still error occurs.Please provide steps suitable for running in windows in command prompt to rectify this mistake.
Is you java file in C:\Users\Neeraj\Prg or is it in C:\Users\Neeraj\Prg\net\sf\fmj\ejmf\toolkit\media\ ?
Packages in java are related to directories. So your classpath is the top level directory in which packages live. A class with the package name net.sf.fmj.ejmf.toolkit should be in that directory.

ClassNotFoundException when connecting to Mysql with JDBC

I'm getting the following error when I try to run a simple Java JDBC program at the command line:
Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver/java
Caused by: java.lang.ClassNotFoundException: LoadDriver.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
Here's the simple Java program, copied right out of the JDBC docs:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Notice, do not import com.mysql.jdbc.*
// or you will have problems!
public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
throw ex;
// handle the error
}
}
}
Problem is, I'm bloody sure my bash shell $ClASSPATH variable is pointed at the correct .jar file. To be sure, I copied the JDBC .jar to the same directory as my program and ran it as follows:
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
I still get the same error.
Edit:
I followed Powerlord's suggestion below, and now I am still getting virtually the same exception.
I entered:
javac -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
java LoadDriver
Whether or not I leave the classpath flag on the second command seems not to matter. I am still getting:
Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver
Caused by: java.lang.ClassNotFoundException: LoadDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
I think your syntax is just wrong here. Have you already compiled LoadDriver.java using:
javac -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java ?
If so, then you should be able to do :
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver
(Note that I removed the '.java' from the end)
Short version:
javac requires you to put the .java at the end, java requires you to not put the .java at the end.
As Jim Garrison noted before he deleted his answer, this command-line to run the program is wrong.
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
This tells Java to load LoadDriver/java.class
What you actually want is
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver
Provided of course that you compile it first with
javac -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
The problem is not the missing driver, it's the missing LoadDriver class. You need to compile the .java source file to a .class file first:
javac LoadDriver.java

Categories

Resources