MemoryMeter : Instrumentation is not set; Jamm must be set as -javaagent - java

After cloning jamm and setting the maven URL in the build.xml to https://repo1.maven.org/maven2/org/apache/ivy/ivy, I was able to install it with ant jar. Now I'm trying to use MemoryMeter in my project code.
The official GitHub README says: 'To use MemoryMeter, start the JVM with "-javaagent:/jamm.jar"'.
Following this advice and using a javaagent for the first time in my life (in a Grails 2.5.6 project) I've set the following in my IDEA IntelliJ run configuration:
in VM options: -javaagent:"/home/myusername/git/laser-ygor/lib/jamm-0.3.4-SNAPSHOT.jar"
in Command line: -noreloading -javaagent:"/home/myusername/git/laser-ygor/lib/jamm-0.3.4-SNAPSHOT.jar" run-app
I think one of those two javaagent settings should be obsolete, but I don't know which of them works, and I believe it won't do any harm to set them both.
Anyway, I'm experiencing: Instrumentation is not set; Jamm must be set as -javaagent.
which obviously is caused by instrumentation being null.
So, how can I use this javaagent with instrumentation being not null?

Related

How do I obtain classpaths from a VirtualMachine?

I have a VirtualMachine from which I want to extract "classpaths". The VM in this case is launched using a RawCommandLineLauncher.
The closest thing I could find is the VirtualMachine method allClasses, but I would like one step above that and get a list of class directories instead. Is this possible?
I tried:
Obtaining a ClassLoader from the VM. Don't think this is possible.
Using the JDI implementation for this. Unfortunately the method is not public.
You can check and cast to a subinterface, PathSearchingVirtualMachine
if (vm instanceof PathSearchingVirtualMachine psvm) {
List<String> cp = psvm.classPath();
}
Above example uses instanceof pattern matching, which is a newer language feature. If you're on an older version of Java then do a normal cast.
The jinfo <pid> command offers some insight. For example it contains the following for a running eclipse instance for me:
[...]
VM Arguments:
[...]
java_class_path (initial): C:\foo\bar\eclipse\jee-2022-06\eclipse\\plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar
[...]
However this is only the initial class path when the application was started. Depending on the application it may create additional classloaders on runtime information and hence extend the classpath during runtime. For example if you are running a spring boot fat jar, the initial classpath will contain the fat jar and hence all of the additional classes besides the jvm classes itself. However this fat jar is in the spring boot format and the actual classpath consists of the jars contained in the fat jar.

"Class not found" when runing feature with karate.jar

I have a feature that execute a function from a Java class. So for this reason I use this command to get the class and create a new instance:
When I run this feature with maven (mvn test -Dtest...) everything is okay. The problem is when I run this feature with karate standalone jar, karate can't find the RCNUtils class.
Error:
org.graalvm.polyglot.PolyglotException: TypeError: Access to host class utilities.RCNUtils is not allowed or does not exist.
GraalJS error: https://github.com/oracle/graaljs/blob/master/docs/user/FAQ.md#typeerror-access-to-host-class-commyexamplemyclass-is-not-allowed-or-does-not-exist
I think it's a classpath problem but I have tried with a lot of differents paths and commands to execute the jar, and nothing works.
I don't know if this is a known issue or if there is a karate example using utilities classes and executed with karate.jar
I tested this problem with differents Karate versions. Actually I'm using Karate 1.1.0
Some of the options I have tried with no results:
Use -w / --workdir param to change working directory with no results
Use java -cp instead of java -jar to set classpath following:
Unable to use read('classpath:') when running tests with standalone karate.jar
Use -Dkarate.config.dir param
Note: I don't think it's a security problem because if I try to get "RCNUtils.java" file with "karate.read()" or "read()" in the same feature, it works. I think because I can put the path to the file. The problem is that I can't put the path to java class in "Java.type()" method
Same error here: Executing Karate jar with mock using external library Spring Framework
Thanks in advance.

Gradle - Could not find load main class

I need to tune the JVM memory by help of Gradle. I've writen settings in the gradle.properties file:
org.gradle.jvmargs=-XX:MaxMetaspaceSize=60M
but it didn't work. Then I've done it manually in an IDEA (Run>Edit Configurations...) and wrote the settings for the needed class (in VM options: field) and it works. Can we do the same by help of Gradle?
I've tried to do:
org.gradle.jvmargs=-XX:MaxMetaspaceSize=60M test1.Test
but I got the error:
Error: Could not find or load main class test1.Test
When you run your application via IDEa, it runs the application using java ... command and gradle is not involed at all, so whatever you put in build.gradle doesn't matter.
If you want to run your app via gradle, consider using application plugin, which allows you to set jvm args. If you use Spring Boot use can use bootRun task and specify params in jvmArgs property.

Maven Command Line Args in the settings.xml

Good afternoon,
I'm trying to find out whether there is a possibility to outsource Command Line Args in the Settings.xml.
I can't use the MAVEN_OPTS Env-Var and the .mavenrc file, but i need the Settings in every project. It is important that no plugins are used which are not included after the installation (the settings are needed for a connection with the repository).
Settings:
-Djavax.net.ssl.trustStore=VALUE
-Djavax.net.ssl.trustStorePassword=VALUE
-Djavax.net.ssl.keyStore=VALUE
-Djavax.net.ssl.keyStorePassword=VALUE
-Djavax.net.ssl.keyStoreType=VALUE
-Dmaven.wagon.http.ssl.insecure=VALUE
-Dmaven.wagon.http.ssl.allowall=VALUE
-Dmaven.wagon.http.ssl.ignore.validity.dates=VALUE
I hope i haven't overlook somthing obvious...
Thanks in advance,
LocXar
EDIT-1: Maven Version: Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00)
EDIT-2: I have tried it like this, but it does not work: <javax.net.ssl.trustStore>VALUE</javax.net.ssl.trustStore>
The best thing is to use the ${maven.projectBasedir}/.mvn/maven.config if you like to use special configurations elements for Maven itself (for example: -T3 ..). Furthermore you can use things for the JVM via ${maven.projectBasedir}/.mvn/jvm.config which means in your case you should use the jvm.config with the following content:
-Djavax.net.ssl.trustStore=VALUE
-Djavax.net.ssl.trustStorePassword=VALUE
-Djavax.net.ssl.keyStore=VALUE
-Djavax.net.ssl.keyStorePassword=VALUE
-Djavax.net.ssl.keyStoreType=VALUE
-Dmaven.wagon.http.ssl.insecure=VALUE
-Dmaven.wagon.http.ssl.allowall=VALUE
-Dmaven.wagon.http.ssl.ignore.validity.dates=VALUE
So you can put that in your project and check in this within your project. So this makes .mavenrc and MAVEN_OPTS superfluous. This is documented in the Release Notes for Maven 3.3.1.
But I have to say that you like to set the above system properties I got the impression you would like to ignore the certificates within an https connection which does not makes sense.
It seems me you can define all arguments in Maven Settings profile with properties:
https://maven.apache.org/settings.html#Profiles
HIH

Setting properties via Maven command line

I'm confused about the correct way to set a property for some unit tests via the command line when using Maven. There are a number of questions (e.g. Specifying Maven memory parameter without setting MAVEN_OPTS environment variable, Is there a way to pass jvm args via command line to maven?, How to set JVM parameters for Junit Unit Tests?) that touch on this subject but none have the answer I'm looking for.
I want to set the property java.util.logging.config.class to some value but I don't want to set the MAVEN_OPTS environment variable.
I can configure the surefire plugin in my pom file with the property:
<argLine>-Djava.util.logging.config.class=someClass</argLine>
so that it is set every time the test phase is run.
However, if I remove the setting from the pom file and add the following to the command line:
mvn package -DargLine="java.util.logging.config.class=someClass"
then the following error in the test phase is reported and the build fails:
Error: Could not find or load main class java.util.logging.config.class=someClass
If I run the following from the command line:
mvn package -Djava.util.logging.config.class=someClass
then the following error is reported at the beginning of the build but the build and tests are successful:
Logging configuration class "someClass" failed
java.lang.ClassNotFoundException: someClass
I don't really understand the behaviour. Can someone enlighten me?
Yes, you should have
mvn package -DargLine="-Djava.util.logging.config.class=someClass"
Notice the addition of -D inside the argLine.
Let's explain why. argLine is indeed an attribute of the maven-surefire-plugin with the corresponding user property argLine. This means that you can set this property directly on the command line with -DargLine. But then, the value of that property is -Djava.util.logging.config.class=someClass. This is exactly what you had when you configured the plugin in the POM directly with
<argLine>-Djava.util.logging.config.class=someClass</argLine>
Additionally, when you call
mvn package -Djava.util.logging.config.class=someClass
then you are not setting the argLine property. You are adding a system property.
Regarding your approach to configuring tests
If you want to pass in configuration to your tests, use for example a file in src/test/resources/myTestConfig.xml. Or use the enhanced features of Test-ng. Your tests will have no value on the centralized build server, or those who want to run/test your code, where config values can't be changed (easily).
The recommended usage of command-line arguments for Maven is for configuring the Maven plugins, build environment and Java config for the build.
Use as little features, config and plugins as possible. Or you'll face a ton of issues down the line, and the tweak-all-you-want-A-standardized-build-doesn't-mean-sh*t-for-me-Gradle will feel more comfortable.

Categories

Resources