Failing to start java program ClassNotFound exception received - java

Trying to start java program on linux but it fails to locate JSoup jar file. I have checked other questions and found on how to add libraries while compiling and while trying to run it but I am not sure if I am doing it correctly because it still fails to load the library....
I enter the following commands:
root#vps5441:/var/www/var/www/TimetableLoader# javac -cp /var/www/var/www/TimetableLoader/jsoup-1.8.1.jar:/var/www/var/www/TimetableLoader/mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar. TimetableLoader.java
root#vps5441:/var/www/var/www/TimetableLoader# java -cp /var/www/var/www/TimetableLoader/jsoup-1.8.1.jar:/var/www/var/www/TimetableLoader/mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar. -classpath /var/www/var/www/TimetableLoader/ TimetableLoader
And I get the following error when the program tries to access the JSoup library.
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at TimetableLoader.main(TimetableLoader.java:46)
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

I assume jsoup-1.8.1.jar is in the current directory?
You don't generally need both -classpath and -cp. It's probably ignoring the values given by -cp as a result.
Put your own classes ahead of the 3rd-party ones.
You're already in /var/www/var/www/TimetableLoader so keep it simple:-
root#vps5441:/var/www/var/www/TimetableLoader# java -cp .:jsoup-1.8.1.jar:mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar TimetableLoader
(Best to get out of the habit of running things as "root" unless you have a genuine need, just sayin')

Related

Running a java program via nailgun

Can someone please explain how do I start a program called main.java using Nailgun. It's been a complete nightmare for me and I cannot find any sort of tutorial on the web which explains it clearly.
Do I need to include anything extra in my program to run them?
I'm trying to follow the instructions given on the Nailgun website where it says:
Launch the Nailgun server with "java com.martiansoftware.nailgun.NGServer"
But the output I get is:
Error: Could not find or load main class com.martiansoftware.nailgun.NGServer
Also, when I try to run my compiled java program, the output is:
java.lang.ClassNotFoundException: main
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
at com.martiansoftware.nailgun.NGSession.run(NGSession.java:261)
I think the idea with nailgun is that you have to include the classpath when you launch the server, like this:
$ java -cp /usr/share/java/nailgun-0.9.0.jar:path/to/myapp.jar -server com.martiansoftware.nailgun.NGServer 127.0.0.1 &
NGServer started on 127.0.0.1, port 2113.
[1] 22443
$ ng-nailgun com.user4215685.main

Java running code from command line

I'm on Mac OS X 10.9.2 (Mavericks). I have code that uses an external Java library (twitter4j). It runs fine when I run it through NetBeans. However, trying to run almost identical code in the terminal gives me errors.
My directory structure is straightforward- I have a 'src' folder with the .java file and a 'lib' folder with the external .jar files I use.
From the src folder, I call javac -cp "../lib/*" MyProgram.java which seems to work alright. Now, if I call java MyProgram, I get an exception:
Exception in thread "main" java.lang.NoClassDefFoundError: twitter4j/StreamListener
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2693)
at java.lang.Class.privateGetMethodRecursive(Class.java:3040)
at java.lang.Class.getMethod0(Class.java:3010)
at java.lang.Class.getMethod(Class.java:1776)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: twitter4j.StreamListener
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
It seems to have trouble properly importing the external twitter4j library. What am I missing here?
These are my import statements in the code:
import twitter4j.*;, import twitter4j.conf.ConfigurationBuilder;
UPDATE: Using suggestions below, I also tried running it via java -cp "../lib/*" MyProgram which gives: "Could not find or load main class MyProgram"
You need to specify the classpath when running the program as well as when compiling it:
E.g.
java -cp "../lib/*" MyProgram

Conflict between MySQL and mail Java libraries

I have a problem with Java libraries. I'm using javax.mail and mysql-connector.
While compiling I don't have any problems, but if I try to execute the program with sudo:
$sudo java Server -jar mysql-connector-java-5.1.28.jar
It gives me this error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at dbConnect.dbQuery(dbConnect.java:192)
at Server.main(Server.java:39)
while if I try to execute it without sudo:
$java Server -jar mysql-connector-java-5.1.28.jar
It gives me this error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/internet/AddressException
at dbConnect.registration(dbConnect.java:161)
at dbConnect.splitUsrPass(dbConnect.java:87)
at dbConnect.dbQuery(dbConnect.java:196)
at Server.main(Server.java:39)
Caused by: java.lang.ClassNotFoundException: javax.mail.internet.AddressException
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 4 more
I can understand why it doesn't work without administrator privileges (I guess the mail library requires them) but it seems strange to me that it won't find the MySQL library if I give it the administrator privileges.
Does anyone know if this is a known issue?
When you want to run something with Java on the command line, you have two options:
Run a class: java MyClass
Run a jar: java -jar MyJar.jar
What you are trying to do is both, which will result in the -jar-part will be ignored.
I'm guessing that what you want to do is run the class Server, with the MySQL-jar on the classpath. The correct command will then be:
java Server -cp mysql-connector-java-5.1.28.jar
You could also add the directory containing the MySQL-jar to the CLASSPATH environment variable. The reason you get different error messages when using sudo and not is probably because the root user has a different CLASSPATH than your user (your user lacks the javax.mail-jar on the classpath, while root has it.)

JUnit sample program compiling but giving runtime error

I am starting off with JUnit in Ubuntu and tried to execute the sample program given here. I have also followed instructions as given in the same link. The code compiles succesfully but gives runtime error as follows:
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
at TestRunner.main(TestRunner.java:7)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
So far this is what I have got. This error occurs if the $CLASSPATH variable isn't set to the location of the classes required by it.
> echo $CLASSPATH
/home/webyog/workspace/JUNIT/junit-4.11.jar:.
My java files and class files are in /home/webyog/JUNIT_WORKSPACE/ and all the libraries and the JUnit jar file is in /home/webyog/workspace/JUNIT/.
I executed these in order:
javac TestJunit.java TestRunner.java -classpath /home/webyog/workspace/JUNIT/junit-4.11
(This generated 2 files TestRunner.class and TestJunit.class)
java TestRunner -classpath $CLASSPATH
(This gives the error)
What I am I missing? Please help.
In your question, you have /home/webyog/workspace/JUNIT/junit-4.11.jar:. for $CLASSPATH but state:
the JUnit jar file is in /home/webyog/JUNIT/
(note the workspace missing)
That could explain the error you are seeing.
Try with:
cd /home/webyog/JUNIT_WORKSPACE/
java -cp '/home/webyog/JUNIT/junit-4.11.jar:.' TestRunner
Also you might want to check out some build tool to make you life easier (maven, ant, gradle, ...)

Run DiscardServer of Netty 4.0 from downloaded source directory

I am trying to run the DiscardServer module of a Maven compiled program called Netty, which I have downloaded as source code. I'm using a GNU/Linux command line terminal to try and follow the instructions in the manual http://netty.io/wiki/user-guide-for-4.x.html.
I'm assuming I should run DiscardServer in directory example/src/main/java, but when I move to that directory and type
$ java -cp "~/norbert/netty-master/all/target/netty-all-5.0.0.Alpha1-SNAPSHOT.jar:~/norbert/netty-master/all/target/netty-all-5.0.0.Alpha1-SNAPSHOT-sources.jar" io.netty.example.discard.DiscardServer
the response is "Error: Could not find or load main class io.netty.example.discard.DiscardServer"
I know the class can be run from a source directory somehow, because
$ java io.netty.example.discard.DiscardServer
produces
Exception in thread "main" java.lang.NoClassDefFoundError: io/netty/channel/EventLoopGroup
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getMethod0(Class.java:2685)
at java.lang.Class.getMethod(Class.java:1620)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:492)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:484)
Caused by: java.lang.ClassNotFoundException: io.netty.channel.EventLoopGroup
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 6 more
Having downloaded the binary version netty-4.0.13.Final in addition, I was able to start the discard server in directory netty-4.0.13.Final/jar as follows:
$ java -cp "netty-example-4.0.13.Final.jar:netty-transport-4.0.13.Final.jar:netty-common-4.0.13.Final.jar:netty-buffer-4.0.13.Final.jar" io.netty.example.discard.DiscardServer
How can I run the DiscardServer from directory example/src/main/java or the relevant source directory, though? Thanks, any help would be appreciated.
inside the example folder, you should run:
mvn install
then
mvn exec:java -Dexec.mainClass="io.netty.example.discard.DiscardServer"

Categories

Resources