NoClassDefFoundError when compiling a HelloWorld programming to a jar - java

Here's what I've got.
I've got my 'MyJava' folder which everything is contained in.
MyJava/src/a/HelloWorld.java
MyJava/src/b/Inner.java
MyJava/bin/
MyJava/manifest.txt
HelloWorld.java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
Inner myInner = new Inner();
myInner.myInner();
}
}
Inner.java:
public class Inner {
public void myInner() {
System.out.println("Inner Method");
}
}
Manifest.txt:
Main-Class: HelloWorld
First I compile the .javas to .class:
javac -d bin src/a/HelloWorld.java src/b/Inner.java
Now I put these into a .jar file
jar cvfm myTwo.jar manifest.txt bin/*.class
now I try run the jar:
java -jar myTwo.jar
And I get:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
...
Could not find the main class: HelloWorld. Program will exit.
I know this is a pretty simple problem, what am I missing?

If you examine the files inside your .JAR you will notice that your compiled classes are inside the bin directory (and therefore cannot be found, since your manifest references a class in the top level).
Change your jar... command like this:
jar cvfm myTwo.jar manifest.txt -C bin .
See also the "Creating a JAR File" section of the Java Tutorial.

One of the solutions is to add the following line to the manifest.txt
Class-Path: bin/
Then you can use 'your' command for the jar creation:
jar cvfm myTwo.jar manifest.txt bin/*.class

Related

class file in JAR seems to be ignored; class file being picked up from directory on classpath instead

I am trying to get a simple JAR file executing on the CLI (without a manifest). The folder structure is as follows:
mods\un.jar
out\test\Main.class
src\test\Main.java
Here is Main.java:
package test;
public class Main{
public static void main(String []args){
System.out.println("Unnamed module...");
}
}
Here are the commands from the root folder:
javac -d out src\Main.java
java -cp out test.Main - this works
jar -cvf mods\un.jar out\test\Main.class
java -cp mods\un.jar;out test.Main - works (when 'out' is in classpath)
java -cp mods\un.jar test.Main - ERROR
The last line generates the errors:
Error: Could not find or load main class test.Main
Caused by: java.lang.ClassNotFoundException: test.Main
I have looked at the JAR zip and it looks ok. The MANIFEST.MF is the default one but that is okay as I am explicitly telling Java what class to start with (the class with main()). The contents are:
META-INF\MANIFEST.MF
out\test\Main.class
The manifest itself is:
Manifest-Version: 1.0
Created-By: 11 (Oracle Corporation)
So it looks like the class file in the JAR is not being picked up at all. If I omit the out folder when I run java or if I rename the .class file in the out folder), I get the class not found error. I would like to get it working without a manifest.
Any ideas?
Thanks,
Seán.
You need to change to the 'out' directory to get your package root correct in the jar:
jar -cvf mods\un.jar -C out test\Main.class

Java Agent not running when packaged into jar file

I am trying to implement a java agent to run before an application jar, for PoC i have created 2 Java Projects in Eclipse, one is app.java and agent.java,
app.java
package test_app;
public class app {
public static void main(String args[]) {
System.out.println("MAIN JAVA CLASS!!!!");
}
}
agent.java
package java_agent;
import java.lang.instrument.*;
public class agent {
public static void premain(String agentArgs, Instrumentation inst) {
System.out.println("INSIDE AGENT");
}
}
my manifest file for app.java looks like this,
Main-Class: test_app.app
Class-Path: .
my manifest file for agent.java looks like this,
PreMain-Class: java_agent.agent
I have executed for both with these commands,
javac app.java
javac agent.java
jar -cvfm app.jar manifest.txt app.class
jar -cvfm agent.jar manifest.txt agent.class
then I am copying the .jar file sinto another directory and running,
java -javaagent:agent.jar -jar app.jar
but the combination, it not working and giving errors like this,
Exception in thread "main" java.lang.ClassNotFoundException: agent_app.agent
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:431)
at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:525)
*** java.lang.instrument ASSERTION FAILED ***: "result" with message agent load/premain call failed at ./open/src/java.instrument/share/native/libinstrument/JPLISAgent.c line: 422
FATAL ERROR in native method: processing of -javaagent failed, processJavaStart failed
Kindly help me to fix this and how to do it. I am doing entirely on Eclipse.
It's obviously: you should move your .class file into your specified package(path), and which are test_app and java_agent.
After you have done this, make the jar and run, it should be ok.
Besides, why don't you make the jar using eclipse?

endorsed dir mechanism not working?

I need to debug something happening inside JDK 1.8 classes. I'm trying to use the endorsed dir mechanism mentioned here, but doesn't seem to be working for me. This is what I did (on Windows):
copied the javax.swing.plaf.basic.BasicListUI class source code into <JDK_HOME>/jre/lib/endorsed/javax/swing/plaf/basic/
Modified the class code, adding:
static {
System.out.println("REPLACED BasicListUI");
}
I expect this to cause the message to be printed as soon as the class is loaded.
cd to <JDK_HOME>/jre/lib/endorsed and run javac javax/swing/plaf/basic/BasicListUI. This results in the BasicListUI.class file generated in the same dir as the source file.
wrote, and compiled this program:
import javax.swing.plaf.basic.BasicListUI;
public class t {
public static void main(String args[]) {
System.out.println(System.getProperty("java.endorsed.dirs"));
System.out.println(BasicListUI.class.getResource("BasicListUI.class"));
}
}
executed program with java t. I don't see the REPLACED BasicListUI message, and the output of the program is:
C:\Program Files\Java\jdk1.8.0_40\jre\lib\endorsed
jar:file:/C:/Program%20Files/Java/jdk1.8.0_40/jre/lib/rt.jar!/javax/swing/plaf/basic/BasicListUI.class
Also, if I run:
$ java -verbose t | grep BasicListUI
The output is:
[Loaded javax.swing.plaf.basic.BasicListUI from C:\Program Files\Java\jdk1.8.0_40\jre\lib\rt.jar] jar:file:/C:/Program%20Files/Java/jdk1.8.0_40/jre/lib/rt.jar!/javax/swing /plaf/basic/BasicListUI.class
What am I missing?
Ah, found the problem. The classes have to be packaged in a jar file. so, added
jar cvf rt.jar javax/swing/plaf/basic/BasicListUI.class

Error: Could not find or load main class SOAPTester

This should be an easy one I have the following code...
package org.me.test;
public class SOAPTester {
public static void main(String[] args) throws Exception{
}
}
Eclipse compiles the classes and puts them in the bin so I go into the bin folder and I tried...
java -cp . SOAPTester
java -classpath . SOAPTester
I went back a folder and tried...
java -cp ./bin/org/me/test/SOAPTester.class SOAPTester
All of these return...
Error: Could not find or load main class SOAPTester
Can someone tell me what I am missing here? This is on JDK7 and I can confirm the .class file is in the folder.
From the bin folder, try:
java -cp . org.me.test.SOAPTester
You'll have to execute following when you're in root folder of org folder
java org.me.test.SOAPTester
More details from Why can't I run my java Hello World program if it is inside a package?

Java can't find the main class

My java can't find the main class why.helloworld. and I can't figure out why. This is my code:
package why;
public class Helloworld {
public static void main(String[] args) {
System.out.println("viva");
}
}
Environment variables:
CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
JAVA_HOME:C:\Program Files\Java\jdk1.7.0
Path :%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
Unfortunately you have not sent your command line but I guess that you forgot to write the package name when running your application. Use command line:
java -cp YOUR_CLASSPATH why.Helloworld
run it from your project directory. The YOUR_CLASSPATH should be either . or name of your jar file or path to directory where your classes are. Probably it is classes or bin depending on your project structure.

Categories

Resources