Jar Class not found upon execution - java

I am trying to use the javax.jms library: https://docs.oracle.com/javaee/7/api/javax/jms/package-summary.html, for which I have downloaded the jar: http://www.java2s.com/Code/Jar/j/Downloadjavaxjms111sourcesjar.htm
Specifically, I am trying to use the MessageListener and Message classes, which I know to be in there based on the Jar decompilation. I put this jar file next to my java file, so that the file structure looks like this:
myDir
-|jms.jar
-|Main.java
Main.java:
import javax.jms.MessageListener;
import javax.jms.Message;
public class Main {
public static void main(String[] args) {
MessageListener listener = new MessageListener() {
#Override
public void onMessage(Message msg) {
}
};
System.out.println("Hello World");
}
}
I can compile this using javac -cp jms.jar Main.java from inside myDir. This creates Main.class. However, when I run java Main, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/jms/MessageListener
This would lead me to believe that MessageListener is not included in the jar, but it is and the file structure is javax/jms/MessageListener checks out. What dumb mistake am I making?
When I compile without -cp js, it fails, saying:
error: package javax.jms does not exist
thus, at least the compiler is looking in the jar.

I made 2 Mistakes
1: Credit to #NormR, .:jms.jar (or .; for Windows I surmise)
2: Link jar while executing. Therefore, the commands should've been:
javac -cp .:jms.jar Main.java
java -cp .:jms.jar Main

Related

How to include a third-party jar-file in compilation and runtime using command prompt in Windows 10?

I'm trying to understand the inclusion of third party jar files in a java project using only the command line in Windows 10.
Specifically, I try to include the file json-20200518.jar in my "project" so that I can use the java object JSONObject in the project.
My java file:
package com.mypackage.example;
import org.json.JSONObject;
class Example {
public static void main(String[] args) {
// ... program logic
}
}
location of my java file (Examp.java):
./com/mypackage/example
location of jar file:
./jars
using cmd win10 I compile:
javac -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Examp.java"
compilation is successful.
Run:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" com.mypackage.example.Examp
I get a report:
Error: Could not find or load main class com.mypackage.example.Pokus
Caused by: java.lang.ClassNotFoundException: com.mypackage.example.Pokus
Second attempt:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Pokus"
But the same error message comes back to me.
Where am I going wrong? Is it the wrong structure? I don't get it, the compilation is successful but the run does not work.
The compiled Examp.class file isn't part of json-20200518.jar, so you'll need to add the directory containing it to the command line. Assuming it's the current directory (.):
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar;." com.mypackage.example.Examp

Compiling java code in OS X Terminal without cd

When I run
javac 'path/to/test.java'
java 'path/to/test'
I get an error like this
Exception in thread "main" java.lang.NoClassDefFoundError: path/to/test (wrong name: test)
It works when I do the same after I run "cd path/to", but is there a way to do this without the cd command? If so, how?
If you wish to compile and run the class from your current location (presumably a project directory), I would recommend:
Use a package declaration in your class:
package path.to;
public class Test {
public static void main(String[] args) {
System.out.println("LOADED");
}
}
When you run your code do so with dot notation to the class name:
javac path/to/Test.java
java path.to.Test
Your Test.java file should have a package line at the top:
package path.to;
public class Test {
public static void main(String argv[]) {
}
}
Then you can compile and run it. It works on my MacBook Pro. Note that java path.to.Test and java path/to/Test are equivalent.
% javac path/to/Test.java
% java path.to.Test
If you are missing the package statement, then you will get the error that you are reporting.
% javac path/to/Test.java
% java path.to.Test
Exception in thread "main" java.lang.NoClassDefFoundError: path/to/Test (wrong name: Test)
Assuming test.java does not have a package statement, your class (which should be named Test, not test) is in the unnamed package, so do this:
javac 'path/to/test.java'
java -cp 'path/to' test
If your test.java file starts with a package to; statement, then you do this:
java -cp 'path' to.test
And if it says package path.to;, then you do this:
java path.to.test
or this to be explicit:
java -cp . path.to.test
If your package statement says any other name, then your directory structure is wrong, since the directory must match the package statement, starting at the directory named in the classpath (-cp).

Cannot find output .class files using javac?

I type javac helloworld.java at cmd in win 7.
C:\Users\User\Downloads\java_tut>javac HelloWorld.java
C:\Users\User\Downloads\java_tut>dir *.class
Directory of C:\Users\User\Downloads\java_tut
03/28/2014 05:42 PM 429 YourClassName.class
C:\Users\User\Downloads\java_tut>
I searched the following directories for helloworld.class:
java, jre, jdk, java_tut, jre/bin, jdk/bin, and my entire harddrive.
I did need to manually add the /jdk/bin directory to my path. I wonder if that matters?
Another possible reason is an empty .java source file.
This causes javac to silently produce nothing.
I experienced that effect with a Hello Word program and a Macintosh editor - which would save with Cmd-S, but does not save with Ctrl-S. I learned this after 20 years of Java programming.
If HelloWorld.java compiled without any errors, then the file HelloWorld.class should definitely be in the java_tut directory.
EDIT (based on your comments and edits):
Check if your Java source file HelloWorld.java looks as follows:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hellow, World!");
}
}
The class must have the same name as the Java source file or you get following compiler error message:
[515]% javac HelloWorld.java
HelloWorld.java:1: error: class YourClassName is public, should be declared in a file named YourClassName.java
public class YourClassName {
^
1 error
Although I asked about the package declaration, I can tell you the correct approach:
Let's assume you have a Java class with that source:
package my.test;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World.");
}
}
Assuming your project root directory is
C:\Projects\java_tut
you must put the source file HelloWorld.java into the directory
C:\Projects\java_tut\my\test
Afterwards you compile and start this little program while being in the java_tut directory with the following commands:
C:\Projects\java_tut> javac my/test/HelloWorld.java
C:\Projects\java_tut> dir my\test
[...]
28.03.2014 09:35 <DIR> .
28.03.2014 09:35 <DIR> ..
28.03.2014 09:35 434 HelloWorld.class
28.03.2014 09:34 134 HelloWorld.java
[...]
C:\Projects\java_tut> java my.test.HelloWorld
Hello World.
Explanation: If working with packages (and you always should use packages for your classes) you must not "sit" in that package, but always run the commands from outside the package (folder).
YourClassName.class is the correct file in this case. The class name isn't generated based on the .java file's name. It's generated based on the class name inside the .java file. In my .java file, I named the class YourClassName and not HelloWorld.

NoClassDefFoundError on simple jar file

public class RunScript {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I created a Java project and a package called com.klong
Inside of the package I have one .java file with the above code in it.
I export it into a runnable jar file. Then I try to run the jar in a command line using this:
java test.jar
When I try that, I get the following error
C:\Users\IBM_ADMIN\Tracing stuff>java test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: test.jar
Caused by: java.lang.ClassNotFoundException: test.jar
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:653)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:619)
Could not find the main class: test.jar. Program will exit.
I've tinkered with exporting into a normal jar file and such. I've looked at other questions about this error. I've tried using commands such as set classpath=BLEH
This project is as simple as can be so hopefully you can help me figure out this pesky error!
You need to run java -jar test.jar, what you're currently doing is asking java to look for compiled classes with the name test.jar.

Understanding JARs and Packages in Java

I'm trying to understand how jars and packages work in Java. So to do this, I created a simple test JAR and am trying to use a class contained in that jar. Simple enough, but it is giving me errors like "class not found". Here's the setup:
1) I have a file called MyHelloWorld.java, which will be packaged in a JAR:
package com.mytest;
public class MyHelloWorld {
public String getHello() {
return "Hello";
}
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
2) I have another file called 'HelloHello.java' which uses the function getHello() in com.mytest.MyHelloWorld
import com.mytest.*;
public class HelloHello {
public static void main (String[] args) {
MyHelloWorld hello = new MyHelloWorld();
System.out.println(hello.getHello());
}
}
3) To package the MyHelloWorld class inside a JAR, I created the folders com/mytest in the current directory, and moved MyHelloWorld.java to that folder
4) I compiled MyHelloWorld.java in that folder using javac MyHelloWorld.java
5) I ran jar -cf myhello.jar ./com/mytest/*.class from the root folder to create the JAR file (as described in http://www.javacoffeebreak.com/faq/faq0028.html)
6) I copied HelloHello.java and myhello.jar to a new folder with nothing else in it, to test this setup
7) javac -cp ./*.jar HelloHello.java [succeeds]
8) java -cp ./*.jar HelloHello [FAILS] (I also tried just `java HelloWorld', which failed too, with a different error message)
This last statement fails with the message:
$java -cp ./*.jar HelloHello
Exception in thread "main" java.lang.NoClassDefFoundError: HelloHello
Caused by: java.lang.ClassNotFoundException: HelloHello
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)
Any idea why it's failing? Any insights you can provide on why it works this way, and how package names are defined inside a JAR etc. would also be appreciated!
I believe it is looking in the jar for your HelloHello class. You probably need the current folder on the classpath too.
java -cp .:myhello.jar HelloHello
You should use:
java -cp .:./* HelloHello
java and javac treat -cp argument a bit differently. With java the * in cp will automatically load all the jars it finds in the given location.
Also, the colon : is the separator between different classpath elements.
Make sure if HelloHello.class is in appropriate directories structure (com/mytest) than change your 8th step:
8) java com.mytest.HelloHello //or java -cp .;*.jar com.mytest.HelloHello
well, java HelloHello works too

Categories