I'm trying to import a .jar file in my project whitout using IDE and the way I'm doing everything works fine until I run the program. I'm getting the following error message when I run it.
So, I created this bar-project folder which I created the .jar file.
bar-project/resources
bar-project/src/com/bar/packmain/Bar.java
Bar.java:
package com.bar.packmain;
public class Bar {
public Bar() {
System.out.println("Bar.");
}
}
At the folder bar-project/src/ I'm compiling like this:
javac -d . com/bar/packmain/Bar.java
To create the jar file I'm using the follwing command:
jar -cvf Bar.jar com/bar/packmain/Bar.class
Then I move this .jar to my other project called foo-project.
foo-project/resources
foo-project/library/Bar.jar
foo-project/src/com/foo/packmain/Foo.java
Foo.java:
package com.foo.packmain;
import com.bar.packmain.Bar;
public class Foo {
private Bar bar;
public Foo() {
new Bar();
}
public static void main(String[] args) {
new Foo();
}
}
At the foo-project/src/ folder I'm compiling and running like this:
javac -cp .:../library/Bar.jar -d . com/foo/packmain/Foo.java
java com.foo.packmain.Foo
Exception message:
Exception in thread "main" java.lang.NoClassDefFoundError: com/bar/packmain/Bar
at com.foo.packmain.Foo.<init>(Foo.java:10)
at com.foo.packmain.Foo.main(Foo.java:14)
Caused by: java.lang.ClassNotFoundException: com.bar.packmain.Bar
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 2 more
So, my question is how can I fix this?
You compiled the project without an error:
javac -cp .:../library/Bar.jar -d . com/foo/packmain/Foo.java
But your command to run your application doesn't include the Bar.jar in classpath.
You have to include your JAR file here, too:
java -cp .:../library/Bar.jar com.foo.packmain.Foo
This is a good way to learn how these things work at the lower level and you get a better understanding what higher levels tools like an IDE or Maven really do.
Related
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?
I have a java file ComPac.java with the below code:
package com;
public class ComPac{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
The file is located at the path : /home/ec2-user/java_c
To compile this file, I ran javac Compac.java, and the class file was generated.
Now its turn to run the class file.
So I did java ComPac(screenshot below)
Understandably, I got the error Error: Could not find or load main class ComPac. Caused by: java.lang.NoClassDefFoundError: com/ComPac (wrong name: ComPac).
This I am assuming is because the java file has the package com declared in it.
So instead I tried, java com.ComPac and expected it to work(screenshot below).
But I got the error: Error: Could not find or load main class com.ComPac. Caused by: java.lang.ClassNotFoundException: com.ComPac.
So how do I run this? and what exactly is the logic for running when it comes to packages in java?
Java used- openjdk version "11.0.8" 2020-07-14 LTS(AWS Corretto)
OS used- Amazon Linux 2
put the class in a folder called "com"
in a bash shell it's then:
$ java com/ComPac
(from the folder containing "com", not inside of "com")
If you are using Java 11 then you don't need to first compile java file and then run the class file. You can directly run the java file using just
java .\test.java
test.java
package com;
public class test{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
command:
java .\test.java
Output:
Hello World
The correct way of doing it as follows:
javac -d . ComPac.java
The switch -d . asks the compiler to place generated class files at the current directory as . stands for the current directory. Learn more about the switches by simply using the command javac
Now, if you use the command ls in Mac/Unix or dir in Windows, you will see a directory, com has been created and ComPac.class has been placed inside this directory. In order to execute the class, you can now use the following command:
java com.ComPac
In NetBeans 8.0.2 I've done a simple "Hello World" class that compiles and everything is fine.
public class OOP_HW3 {
public static void main(String[] args) {
System.out.println("Hi ");
}
}
On the other hand when I navigate to this folder with console and run:
$ javac OOP_HW3.java // OK
$ java OOP_HW3
Exception in thread "main" java.lang.NoClassDefFoundError: OOP_HW3 (wrong name: oop_hw3/OOP_HW3)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
....
I can easily run this file with console in any other directory, but here in the NetBeans project gives me error. What am I missing ?
Run the following commands from where you are running current command like:
cd ..
java oop_hw3.OOP_HW3
Since you are using package, you need to be on one top i.e. parent directory and then run with fully qualified package name followed by class where you define main method.
Remove the package definition from the top of your java file.
I created the following class located in the MainJPrint.java file
import com.XXXXX.pdfPrint.PDFPrint;
public class MainJPrint
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
print(".....");
}
public static String print (final String url)
{
Object rc = AccessController.doPrivileged(new java.security.PrivilegedAction()
{
public Object run()
{
...
}
}
}
}
In the same folder I have a jar archive jPrint.jar
I compile the class using the following command
>javac -classpath jPrint.jar MainJPrint.java
When I'm trying to execute resulted class file, I get this error:
>java MainJPrint
java.lang.NoClassDefFoundError: com/XXXXX/pdfPrint/PDFPrint
If I uncomment the Hello World line and comment the next line, the program runs fine.
I'm using j2sdk1.4.2 installed at C:\j2sdk1.4.2.
I do also have installed other java versions (at C:\Program Files\Java: jre 1.6.0_01, jre 1.6.0_02, j2re1.4.2, jre6, jre7, jdk1.7.0_03)
The PATH variable contains the C:\j2sdk1.4.2\bin path, however I think the java.exe is loaded from the upper version, but it shouldn't matter and I can call it like
>C:\j2sdk1.4.2\bin\java.exe MainJPrint
jPrint.jar is a third party archive and I need to create an applet which exposes a method so I can call it with javascript. I'm not a java developer, I'm having some little troubles and I'm really on an end here.
I tried other options like:
>java MainJPrint -cp .
>java MainJPrint -cp jPrint.jar
So how can I execute that class file which uses a class located in a separate archive?
To execute a class that depends on external JARs, you need to specify all elements of the classpath on the command line.
If you don't specify a classpath, Java automatically uses . (the current directory), which is why, if MainJPrint didn't depend on jPrint.jar, your invocation java MainJPrint would have worked.
But when you specify -cp jPrint.jar, Java does NOT automatically add the current directory to the classpath, which means that it then cannot find MainJPrint. You need to specify both. On Mac/*nix, the following invocation should work:
java -cp jPrint.jar:. MainJPrint
Or on Windows:
java -cp jPrint.jar;. MainJPrint
I am trying to run simple java code on VMWare Workstation. I have the following simple test Main file:
import cern.jet.random.engine.RandomSeedGenerator;;
public class TestDataService {
//private static Logger logger = Logger.getLogger(TestDataService.class);
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World DAI!");
// Input some data.
RandomSeedGenerator re = new RandomSeedGenerator();
return;
}
}
RandomSeedGenerator is a class in colt.jar library, and I have the jar file under my lib folder.
I am building the project with ant, and I have the following manifest file where I set the classpath:
Manifest-Version: 1.0
Main-Class: edu.umass.TestDataService
Name: edu/umass/TestDataService/Version.class
Class-Path: lib/colt.jar
When I run the code from the VMWare shell which runs Red Hat Linux, I get this Exception:
[root#localhost] java -jar app.jar
Hello World DAI!
Exception in thread "main" java.lang.NoClassDefFoundError: cern/jet/random/engine/RandomSeedGenerator
at edu.umass.TestDataService.main (Unknown Source)
Caused by: java.long.ClassNotFoundException: cern.jet.random.engine.RandomSeedGenerator
Just as a final note, everything seems to work fine on windows with eclipse, but nothing seems to work on the virtual machine. Any ideas?
Did you install the jar files required by your application on the VMs?
Did you configured CLASS_PATH correctly?
I doubt there is an issue with the jvm or the vm. The problem is going to be in how you run the class. Specifically how your setting the classpath. Try this:
Navigate to where you've placed colt.jar. Get the present working directory by typing in pwd. Use this to construct the run command using the absolute path to colt.jar.
So eventually you should be running (from the directory containing your jar) something like this:
java -cp /the/full/path/to/lib/colt.jar -jar app.jar
Once you've got that work you can then try and figure out what the correct relative path is. and then you'll be able to do
java -cp a/relativel/path/to/lib/colt.jar -jar app.jar