Compile slf4j in Unix - java

I have a Java program that uses slf4j-api-1.7.10.jar and slf4j-simple-1.7.10.jar. I have tried to compile it in my Unix shell like so:
javac -cp "$CLASSPATH:.:slf4j-api-1.7.10.jar:slf4j-simple-1.7.10.jar" Test.java
Running java Test results in this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at SystemCommandExecutor.<clinit>(SystemCommandExecutor.java:13)
at Test.main(Test.java:12)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
... 2 more
I am able to run the program in Windows with the two jar dependencies, so I don't believe I need any extra jars. Perhaps I am issuing the wrong command to compile in Unix as I am fairly unfamiliar with the syntax.

Why you don't create a pom.xml for compile it with a maven dependency?
In that case you should include the package for the main class:
javac -classpath external.jar com/yourpackage/yourMainClass.java
Also check your syntax for the slf4j inclusion.

Related

Runtime error loading Interface definition in Java using Apache HTTP library

I am attempting to make a simple wrapper around an API connection in Java. I am using the org.apache.http library to do this.
I have downloaded the following jar files:
org.apache.http httpclient saved as ./org.apache.httpclient.jar
org.apache.http httpcore saved as ./org.apache.httpcore.jar
I can then successfully compile the file with:
$ javac -cp "./org.apache.httpclient.jar:./org.apache.httpcore.jar" Api.java
However when running it with $ java Api I receive:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
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)
... 6 more
[It turned out the issue was with how code was being run after compilation; the code has been removed to avoid distracting from the actual problem and solution]
I can't see why the system has a problem finding the HttpEntity interface when it finds other imports successfully, and can compile. If I remove any of the import lines compilation fails with unknown symbol errors.
-cp should contain the directory where you classes are, for example:
java -cp "./org.apache.httpclient.jar:./org.apache.httpcore.jar:." Api
Assuming that your Api.class is in the current directory. (replace : by ; as classpath separator if your are on a windows system)
Have you tried running the java with '-classpath'. While compiling you are giving the classpath, so you need to add the classpath option for running the class file as well.
More details can be found at below link:
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
Also you can print which libs are in classpath using below command.
java -cp $(for i in lib/*.jar ; do echo -n $i: ; done). com.test.Class

java - error while running compiled program with external jar

I have a simple jsoup test app in a single folder, in which there are 2 classes - LyricsGetter.java and Main.java - and a .jar file with jsoup library. When I compile the files with command javac -cp jsoup-1.8.2.jar LyricsGetter.java Main.java everything compiles ok, but when I try to run with java Main, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at LyricsGetter.getLyrics(LyricsGetter.java:16)
at Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
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)
... 2 more
On the other hand if I run with java -cp jsoup-1.8.2.jar Main, then I get Error: Could not find or load main class Main. So, what is the correct way to run this program?
You need to tell Java to look for classes in jsoup-1.8.2.jar, and the current folder (.).
On Windows, use:
java -cp jsoup-1.8.2.jar;. Main
or on Linux, OSX, or other Unix-like systems use:
java -cp jsoup-1.8.2.jar:. Main
(The difference is that the paths are separated by ; on Windows or : on *nix)

Works in sbt but not raw Java: after using `javac` to compile a .class file, why can't `java` find it?

I used ObjectWeb's ASMifier to get a 'HelloDump.java', and added classloader and a main method to load and run a spoofed "HelloWorld".
If I run 'HelloDump.java' in the build tool sbt, everything works fine and outputs "HelloWorld!".
But if I use raw Java, it breaks. 'HelloDump.java' seems to compile OK, but the resulting 'HelloDump.class' is clearly present, yet doesn't seem to be recognized (check the sequence below):
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ javac -cp lib/asm-all-4.1.jar HelloDump.java
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ ls
DumpLoader.java.bak HelloDump.class.bak Hello.java.bak
Hello.class.bak HelloDump$DynamicClassLoader.class lib
HelloDump.class HelloDump.java README.md
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ java -cp lib/asm/all/4.1.jar HelloDump
Exception in thread "main" java.lang.NoClassDefFoundError: HelloDump
Caused by: java.lang.ClassNotFoundException: HelloDump
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: HelloDump. Program will exit.
Explicitly adding . to the classpath doesn't help either.
So why does this work in sbt, but not in raw Java, and how can I fix it?
Thanks, any advice is appreciated,
-Julian
Add . to your CLASSPATH, so your command becomes:
java -cp lib/asm-all-4.1.jar:. HelloDump
The reason the JVM cannot find the class is that it looks only on the classpath and nowhere else.

How to compile and run java files for OpenNI in linux

I'm trying out OpenNI for Kinect and got it to install and run sample code according to
this guide. But now, I want to modify the code and compile it and test it. However, I'm not sure how to compile and run on Linux. I found a guide here that does it for Windows, but can't seem for the life of me to find anything for Linux.
I did try adapting the Windows code for Linux and tried
javac -cp ~/kinect/OpenNI/Samples/Bin/x86-Release/org.OpenNI.jar VersionInfo.java
java -cp ~/kinect/OpenNI/Samples/Bin/x86-Release/org.OpenNI.jar VersionInfo
but it gives me
Exception in thread "main" java.lang.NoClassDefFoundError: VersionInfo
Caused by: java.lang.ClassNotFoundException: VersionInfo
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: VersionInfo. Program will exit.
Any ideas on how I can compile and run my java code onto the Kinect? If you know how to do it for the Samples in the OpenNI folder, that would be perfect.
Nevermind, figured it out. Turns out I needed to learn about make and Makefiles and how they're used to compile .java files into a .jar.

Java Error on Linux OS

Hello Guys I am trying to figure out why i am gettings this error when I am trying to run this API from Shell on my linux System.
[~/public_html/test/]# java -jar jodconverter-cli-2.2.2.jar Amritpal.doc sample.pdf
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/CommandLineParser
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.CommandLineParser
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: com.artofsolving.jodconverter.cli.ConvertDocument. Program will exit.
You need to do this in order to include the Apache Commons CLI library in your classpath:
java -cp '.:/path/to/cli.jar' -jar jodconverter-cli-2.2.2.jar x.doc y.pdf
If you're on Windows, use a semicolon ; instead of the colon : in specifying the classpaths.
Because you don't have the dependencies specified in the classpath (The first one being exactly what the error is telling you, the apache.commons.cli library).
You either need to specify them on the command line or in the manifest in that jar.

Categories