Run DiscardServer of Netty 4.0 from downloaded source directory - java

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"

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

Exception in thread "main" java.lang.NoClassDefFoundError: java/util/function/Predicate

I have created a jar file using
mvn assembly:assembly -DdescriptorId=jar-with-dependencies
and I run it on windows and it works fine and works as expected. Then I run it on Ubuntu and it gives the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: java/util/function/Predicate
at Maxima_ImageJ.run(Maxima_ImageJ.java:13)
at Maxima_ImageJ.main(Maxima_ImageJ.java:27)
Caused by: java.lang.ClassNotFoundException: java.util.function.Predicate
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)
... 2 more
I have no idea why it works differently in Windows and Ubuntu. If someone do, please help. Is it related to the versions of java?
Set class path to Java 1.8 as java.util.function.Predicate is part of JavaSE8 and will not be available in 1.7 some of the set commands to use before executing maven command.
set path=C:\Program Files\Java\jdk1.8.0_05\bin//UPTO Bin
set JRE_HOME=C:\Program Files\Java\jre8//Upto Root folder of JRE
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_05//UPTO Root folder of JDK
Check your java version by using below command.
readlink -f $(which java)
If it is less than 1.8, then you have to update Java_Version.
One way is to edit in .bashrc file.

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

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

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

Categories

Resources