Java running code from command line - java

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

Related

Failing to start java program ClassNotFound exception received

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')

java.lang.ClassNotFoundException when trying to run camus

I downloaded the confluent package which includes camus jars and I followed the instructions online enter link description here.
Hadoop is properly setup (meaning I can use hadoop fs -ls commands and other hadoop jar commands). However, when i tried to run
hadoop jar confluent-camus-1.0.jar com.linkedin.camus.etl.kafka.CamusJob
I got "main" classNotFound error
Exception in thread "main" java.lang.ClassNotFoundException: com.linkedin.camus.
etl.kafka.CamusJob
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 java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:344)
at org.apache.hadoop.util.RunJar.main(RunJar.java:205)
The the path to the "confluent-camus-1.0.jar" is correct (right under the folder). I didn't start the kafka service, just to try to run it.
Anyone got similar problems?
Thanks.
You should try to inspect your jar file:
jar tvf confluent-camus-1.0.jar | grep com.linkedin.camus.etl.kafka.CamusJob
If you do not find this class, try to find it in other jar, which generated by camus.
After you should add target jar with
hadoop jar confluent-camus-1.0.jar com.linkedin.camus.etl.kafka.CamusJob -libjars {JAR_NAME}

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.)

Java program does not run and throws an error about classes

So I want to run my just compiled Start.class file that I compiled from Start.java. The problem is that it will throw this error:
java Start
Exception in thread "main" java.lang.NoClassDefFoundError: Start (wrong name: files/Start)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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 sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
I have written the program in Eclipse and I want to run it from a Terminal. What is the problem here?
New error:
java files.Start http://mirror.symnds.com/software/Apache/tomcat/tomcat-7/v7.0.53/bin/apache-tomcat-7.0.53.zip
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils
at files.Start.main(Start.java:93)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils
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
Please have a look at How to Compile and Run Java Code from a Command Line where you will get the information about the class path and how to add dependent jars in class path.
Here is the details from above link.
Task
Lets say we have a fairly standard java project that consists of three top level folders:
/bin - empty folder that will contain compiled .class files
/lib - contains third party .jar files
/src - contains .java source files`
Our task would be to compile and launch the project from its root folder. We will use Windows OS as example (on Unix systems the only difference would be path separation symbol - ":" instead of ";").
Compiling Java Code
The first step is compiling plain text .java sources into Java Virtual Machine byte code (.class files). This is done with javac utility that comes with JDK.
Assuming we are at the application root folder trying to compile Application.java file from com.example package that uses lib1.jar and lib2.jar libraries from lib folder to a destination bin folder, compilation command should have the following format:
javac -d bin -sourcepath src -cp lib/lib1.jar;lib/lib2.jar src/com/example/Application.java

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