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?
Related
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
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.
I am new to JAVA9 modular architecture and trying to compile and run module inside a JAR from windows command line.
I have created simple HelloWorld.java main class and project architecture is as below :
I was successfully able to compile and create JAR file using following command.
Compile :
javac -d target/HelloWorld src/HelloWorld/com/java/modularity/test1/HelloWorld.java src/HelloWorld/module-info.java
Create a HelloWorld.jar file in "jarfile" directory :
jar -cfe jarfile/HelloWorld.jar com.java.modularity.test1.HelloWorld target/HelloWorld/module-info.class target/HelloWorld/com/java/modularity/test1/HelloWorld.class
Getting following error while trying to run module from JAR file :
D:\sts_workspace\java9tutorial>java -p jarfile -m HelloWorld
module HelloWorld does not have a ModuleMainClass attribute, use -m <module>/<main-class>
Getting following error while slide change in command :
D:\sts_workspace\java9tutorial>java -p jarfile -m target/HelloWorld/com.java.modularity.test1.HelloWorld
Error occurred during initialization of boot layer
java.lang.module.FindException: Module target not found
Here is my entry class HelloWorld.java :
package com.java.modularity.test1;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Testing first HelloWorld module ...");
}
}
Here is HelloWorld module description :
module HelloWorld {
}
I also tried by extracting my generated HelloWorld.jar file and "Main-Class" attribute is also present in MANIFEST.MF file :
Manifest-Version: 1.0
Created-By: 1.8.0_172 (Oracle Corporation)
Main-Class: com.java.modularity.test1.HelloWorld
Do I need to export the entry class in my module definition ? Any suggestion will help me to fix the issue.
Try changing
-m target/HelloWorld/com.java.modularity.test1.HelloWorld
to
-m HelloWorld/com.java.modularity.test1.HelloWorld
for the syntax is module[/mainclass]
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
I've a java program as shown below:
package com.abc.myproject;
import java.util.*;
class test {
public static void main(String[] args)
{
ResourceBundle rb = ResourceBundle.getBundle("mybundle");
String propertyValue = rb.getString("name");
System.out.println("propertyValue="+propertyValue);
}
}
I've this program located under C:\testing\code\src\com\abc\myproject\test.java
And, I've kept mybundle.properties under C:\testing\code folder.
mybundle.properties contain one line:
name=Mike
When I run the program from command prompt as shown below, it runs perfectly fine:
C:\testing\code\src>java -cp .;c:\testing\code com.abc.myproject.test
propertyValue=Mike
Now, I created Jar file xyz.jar containing only test.class & kept this Jar in C:\testing directory & the manifest of jar contains below:
Manifest-Version: 1.0
Created-By: 1.7.0_05 (Oracle Corporation)
Main-Class: com.abc.myproject.test
In the same directory C:\testing, I kept mybundle.properties & then tried to run jar with below commands:
java -cp .;c:\testing -jar xyz.jar
But it fails to load mybundle resource & throws error:
Exception in thread "main" java.util.MissingResourceException: Can't find bundle
for base name scheduler, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)
at java.util.ResourceBundle.getBundleImpl(Unknown Source)
at java.util.ResourceBundle.getBundle(Unknown Source)
at com.abc.myproject.test.main(test.java:8)
I have to keep the mybundle.properties out side of Jar since we should be able to make changes in this file without having to redeploy the Jar.
Can any one please help me in fixing this issue?
Thanks!
java -cp .;c:\testing -jar xyz.jar
You cannot add to the classpath when using -jar. $CLASSPATH and -cp are ignored, and it will only use the classpath in the jar manifest.
What you can do is run your class like this:
java -cp .;c:\testing;xyz.jar com.abc.myproject.test