I start my java program(Test.java) like this:
java -DMyParam=2 Test
I can change this in code using System.setProperty() but is there any command line tool to change value of MyParam property ?
I tried using VM.set_flag but getting below error, probably because set_flag can set flags not system properties.
XXX#XXX-Air:~/javacode$ jcmd 11441 VM.set_flag MyParam 1
11441:
flag MyParam does not exist
This does not seem possible.
You might be able to change some manageable JVM flags using jinfo (https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jinfo.html) but that command is also experimental and maybe no longer supported in next releases according to Oracle.
No, don't think that is possible. If this is the way you want to go, you would probably have to expose that functionality yourself. Perhaps creating a JMX bean and calling it with jmxterm?
Related
I have a java application running in an alpine container. I would like to find a reliable way to determine what is the value of networkaddress.cache.ttl. I tried to use this in the code of the application: java.security.Security.getProperty("networkaddress.cache.ttl"); but it always returns null, even when I use -Dnetworkaddress.cache.ttl=30 in the JAVA_OPTS arguments. So I am doing something wrong obviously but I don't know what. According to this
networkaddress.cache.ttl null in openjdk I don't even need to add the argument in JAVA_OPTS because the default value is 30 anyway. Looking at /usr/local/openjdk-8/jre/lib/security/java.security inside the container I can see #networkaddress.cache.ttl=-1 but it is commented out, so I have no idea what the default value is. So how can i reliably check what is the value of networkaddress.cache.ttl?
For reasons unknown to me -Dnetworkaddress.cache.ttl=30 fails to set anything. I managed to set it by adding this to my Dockerfile
sed -i '313,313 s/^#//' /usr/lib/jvm/zulu8-ca/jre/lib/security/java.security
sed -i '313,313 s/-1/30/' /usr/lib/jvm/zulu8-ca/jre/lib/security/java.security
Another way it can be done is described here. The idea is to use another file for overriding the security property.
Both approaches worked fine for me.
According to the [JDK 11 docs](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/doc-files/net-properties.html):
Since these 2 properties are part of the security policy, they are not set by either the -D option or the System.setProperty() API, instead they are set as security properties.
A note from AWS Java SDK uses SecurityProperty instead of SystemProperty as in:
java.security.Security.setProperty("networkaddress.cache.ttl" , "60");
so you could use
String ttl = java.security.Security.getProperty("networkaddress.cache.ttl");
Summary: Id like to alter the way .hprofs are named when automatically created to incorporate machine's hostname + process PID
Long version: According to documentation (https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/clopts001.html) you can pass an option to the VM to create heapdumps under certain conditions (HeapDumpOnOutOfMemoryError), and can further customize this behavior by specifying a different path for the resulting .hprof file. You can change the name/path. What I would like to know is if its possible to make this option (HeapDumpPath) incorporate the hostname+PID to have uniquely named .hprofs (to be saved in a centralized location, but thats another scope). Could it be done? Can HeapDumpPath accept a parameter to get the hostname?
It doesn't look like there are options to inject values into the path. Depending on your shell you should be able to at least insert the hostname. The PID may not be possible since it is created after the process starts. You may be able to use a different variable like the application name though. In bash it would look something like this:
java -XX:+HeapDumpOnOutOfMemoryError -mn256m -mx512m ConsumeHeap -XX:HeapDumpPath=/disk2/dumps/$(hostname)/${APPLICATION_NAME}
Managed to do it just by using the Windows Environment Variable COMPUTERNAME in the wrapper.conf file for that particular Java VM. The full line is:
wrapper.java.additional.32=-XX:HeapDumpPath=./%COMPUTERNAME%.hprof
I've found tons of documentation on how to enable javax.net.debug for SSL, TLS and similar, but even in the reference documentation I've not found how to totally disable it overriding programmatically a previous setting.
My exact problem is that in a Tomcat instance another application performs the following instruction:
System.setProperty("javax.net.debug","all");
and this causes the catalina.out file to rise his dimension quickly and unwanted.
I've already tried to overwrite it from my Java code with the same instruciton, with "none", "off" and "false" values but with no result, still logging a plenty of TLS stuff.
For example in my application I've added this instruction:
System.setProperty("javax.net.debug","none");
but still I'm getting full log.
The problem is that the tomcat application is overwriting whatever value you give from command line, and if there is no way to control what this code is doing, you can't really overwrite it from commandline arguments. While a security manager would be able to prevent setting a property, it can only do so by throwing an exception, which is probably going to cause more issues than it solves.
In this case, your only option is to set the value yourself from code, after the other code sets it.
In case of the javax.net.debug, the option needs to be set to it's final value before the static static initializer of sun.* Debug class runs, which is before the first message would appear. This can be disabled by any value that isn't used as some option (empty string, or none should disable it). If it's set later, it will have no effect with no way to turn it off after the fact (with the exception of doing some bad reflection hacks to access internals of that class anyway, that are only possible with java 8 and earlier)
If there are some VM argument that enable SSL logging try to remove them, in addition you can check eclipse.ini file to see if those arguments are declared there or not.
You can disable it by removing the following from the run configuration in your IDE:
-Djavax.net.debug=all
To anyone who may need this, I set the value to an empty string: System.setProperty("javax.net.debug","");
It worked for me.
I'm writing a text/code editing program for my own use in Java/Swing, and was wondering how I would go about setting up a built-in C compiler inside it. I would likely use GCC or TCC as the compiler. Anyway, my question is- how would I actually implement the compiler? Would I use a library that gives Java access to command line commands? Are there such libraries and if so, which is the best/easiest to use?
Thanks.
Accessing command line is the easiest way.
Try something like this:
Process myProc = Runtime.getRuntime().exec(command);
Where command is some string you want to pass to the command line.
After that you can redirect output / input of that process to the some java buffers to have the full control.
myProc.getInputStream();
myProc.getOutputStream();
Typically IDE/Editor's don't implement the compilers. They will just execute the commands and pass the filename as argument (along with other necessary files). They also pipe/stream the output to a separate window/pane in the editor. So you need to integrate the compiler somehow not implement one. You can execute the commands in java using Runtime class. Start here.
This question already has answers here:
how to change the name of a Java application process?
(10 answers)
Closed 8 years ago.
If a Java program is started, it get's in the system process-monitor the name java. Many Java-programs are that way hard to distinguish. So it would be nice, if a way exists, to set the name, that will be shown in the process-monitor. I'm aware that this may work different on different Operating Systems.
A simple way would be, if the java-interpreter would support a switch to set the name, like this:
java -processname MyProgram -jar MyProgram
But I couldn't find such a switch, so it is probably non-existant. An API in Java to set the process-name would be also fine.
So, so you have any suggestions?
I don't know if this is possible, but you could use a command line tool that comes with the JDK called 'jps'. It's like *nix ps, but just Java programs instead. jps -v shows all the arguments you have passed to java.
Also, I have seen people attach a "process name" to their java processes by adding an unused -Dmyprocessname to the args.
as #omerkudat said:
jps -v
prints out all java processes {processID, params list}
If the params list is not enough to recognize the applications you need,
try adding some dummy params when running them:
java -Dname=myApp -cp myApp.jar some.client.main.MainFrame
This will print like:
7780 MainFrame -Dname=myApp
and you can use the process ID to kill / monitor it.
You can do this with an LD_PRELOAD shim: https://github.com/airlift/procname
The shim simply calls the Linux-specific prctl() when the process starts:
static void __attribute__ ((constructor)) procname_init()
{
prctl(PR_SET_NAME, "myname");
}
The call has to happen on the main thread, so it isn't possible to do this from Java or even with a JVMTI agent, since those happen on a different thread.
When I first read this, the idea of changing the process name struck me as impossible. However, according to this ancient thread on the sun forum you can use C++ wrappers around the JVM executable to achieve this.
Though frankly, I wonder what your real problem is, as I'd guess there is a more standard solution then attempting to change the process name.
Your best option is something like launch4j
http://launch4j.sourceforge.net/
There is a bug logged in the sun bugtracker for this, but it's not high priority
http://bugs.sun.com/view_bug.do?bug_id=6299778
There are mainly 2 approaches: one is as already described: using tools like Launch4j, WinRun4J to create native Windows launchers.
Another approach that seems better is to use Apache Procrun to wrap the java application as a Windows service. During the install service process, we can give the process an meaningful name such as OurApp.exe.
All we need do is rename prunsrv.exe to OurApp.exe and replace every occurrence of prunsrv.exe in our install|start|stop|uninstall service scripts to MyApp.exe.
See more from Using Apache Procrun to Rename Process Name of a Java Program in Windows
If you want to use a different process name you'll have to create your own binary to launch your Java application using something like JSmooth.
Look at this question for a discussion of creating such binaries.
That's because Java applications aren't actually executable they're ran by the Java virtual machine which is why java appears in the process monitor, it's the host of your application.
Things like LimeWire however do but I think that's more down to GCJ - http://gcc.gnu.org/java/