Included Jsoup library will compile but not run - java

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

Related

java command classpath issue

I am having strange issue in this simple scenario.
I am having a jar file which contains following class:
package com.example;
public class Test{
public void perform(){
System.out.println("Performing testing one");
}
}
And I have created a Main class to call the perform method as followed:
import com.example.Test;
public class Main{
public static void main(String[] args) {
Test test=new Test();
test.perform();
}
}
I have put both the jar and Main.java file in same folder and successfully compiled the Main.java file using following command:
javac -cp ".\*" Main.java
But when I am trying to run the Main class using following command:
java -cp ".\*" Main
It gives following error:
Error: Could not find or load main class Main
If I try to run Main class without -cp argument it gives following error:
Exception in thread "main" java.lang.NoClassDefFoundError:
com/example/Test
at Main.main(Main.java:5) Caused by: java.lang.ClassNotFoundException: com.example.Test
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
The syntax is correct then what I am doing wrong here...?
When using -cp you need to specify jar names one by one. Using * does not usually add anything to the classpath. Good luck!
Everything is wrong.
The source code should be in a directory called com/example.
You should be in the directory containing com.
The compiler command is
javac com/example/Main.java
The execute command is
java com.example.Main
You don't need a JAR file with only one class in it.
After a lot of experiment and searches on net, I have finally found that we need to put ; after classpath to make it work.
So following command does run the Main class:
java -cp ".\*"; Main

Java package run-time error

I am newbie in java. I am running java programs via Ubuntu terminal
I just started java package topic and have been dealing with a problem for several hours.
I tried to create a simple package called pack which has single class Hello.
I created a directory pack. Inside the pack I put Hello.class file intp pack directory via
javac -d ./pack Hello.java
command.
Then I included pack package into a class containing main method. The class's name is test and it is located in test.java file This class is located in another directory. I compile via
javac -cp ./pack test.java
It compiles without any error and everything is ok.
However, when I enter command
java -cp ./pack test
it gives me
Error: Could not find or load main class test
When I tried
java test
command. The following message showed up
Exception in thread "main" java.lang.NoClassDefFoundError: pack/Hello
at test.main(test.java:6)
Caused by: java.lang.ClassNotFoundException: pack.Hello
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
Can anybody explain me what I am doing wrong? Any help is much appreciated.
Sorry, I did bot include my source codes. Here they are.
import pack.Hello;
public class test
{
public static void main(String args[])
{
Hello.HelloMessage();
}
}
This is test.java file which tests if everything is ok. It is located at
/home/uesername/apps
directory.
Then I created "pack" directory. Full path to the pack directory is
home/username/apps/pack
Inside the "pack" I put Hello.java file. The content of Hello.java file is
package pack;
public class Hello
{
public static void HelloMessage()
{
System.out.println("hello, world");
}
}
To begin with I suggest you use an IDE to setup you environment for compiling, running and debugging.
The problem you have is that you have compiled with the wrong path.
javac -cp . pack/Hello.java
javac -cp . pack/test.java
and
java -cp . pack.Hello
or
java -cp . pack.test
The problem is that you compiled a class with package pack in a directory pack and you would end up with
pack/pack/Hello.class
I suggest you check where the Hello.class file has been placed.

Issue with running java class file containing Jsoup dependency

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

How to compile a java program with imported classes?

I am trying to get started with the API of the java software weka. I wrote the following code for testing:
import weka.core.Instances;
import java.io.BufferedReader;
import java.io.FileReader;
public class hello_weka {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("/home/aljoscha/Masterarbeit/weka_examples/iris.arff"));
Instances data = new Instances(reader);
reader.close();
// setting class attribute
data.setClassIndex(data.numAttributes() -1);
System.out.println(data);
System.exit(0);
}
}
It works fine when I execute it in Eclipse.
However I can't get it to run in the Terminal.
I tried to provide the .jar path during compilation and then execute the program from the directory of the compiled class.
javac -cp /usr/share/java/weka.jar hello_weka.java
java hello_weka
This approach does not work, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: weka/core/Instances
at hello_weka.main(hello_weka.java:8)
Caused by: java.lang.ClassNotFoundException: weka.core.Instances
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
What am I doing wrong?
I guess I am doing just some completely stupid stuff since I just start to code in Java. If so, please excuse me and try to tell me how I can do better.
Edit:
when I try the thing proposed in the answers I get the following Error:
Exception in thread "main" java.lang.NoClassDefFoundError: hello_weka
Caused by: java.lang.ClassNotFoundException: hello_weka
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)
Could not find the main class: hello_weka. Program will exit.
Try this out:
java -cp /usr/share/java/weka.jar hello_weka
You need to provide the classpath to your JAR also when executing the program:
java -cp /usr/share/java/weka.jar hello_weka
You need to add also the current directory (where your own classes are stored) to the classpath:
java -cp .;/usr/share/java/weka.jar hello_weka
I finally found the answer: The answer of dunni is nearly correct. For me it works if I provide the classpath of both, the jar file and the classfile, but separated by a :.
java -cp /usr/share/java/weka.jar:/home/aldorado/myjavascripts/ hello_weka
Is it possible that this differs depending on the OS / JDK you are using?

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