How to give command line option in java program? - java

I want to use Java flight recorder while running my java program. Can I pass arguments in java program itself and not while running the application.
For ex: I have a java class say "HelloWorld.class" I want to use java flight recorder while running this which i can achieve with
java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder HelloWorld
But I want to achieve this unlocking commercial feature and start flight recorder from my java code. Is it possible?


Monitoring Locks with Java Flight Recorder and Java Mission Control

What I want to do
I have a Java program which I am trying to improve. I suspect synchronized blocks within the code to hurt performance but I would like to make sure this is my problem before touching my code.
How I went on about it
To check if synchronized blocks are indeed the issue, I recorded the execution of my program on a test server with Flight Recorder, downloaded the created jfr file on my desktop and opened it with Java Mission Control. However the Lock Instances page in Java Application does not show anything. The only clue I get is a message in the Results view which reads:
The Java Blocking rule requires event(s) to be available from the following event types:
I am therefore assuming there must be some kind of option to activate along with the flight recorder but I wasn't able to find it so far.
My question
How do you enable events from the type to be recorded by the Java Flight Recorder ?
Or I am missing something else and there is a better way to figure out how much blocking on synchronized blocks is done in a Java program ?
My environment
I am using Oracle JDK version 1.8.0_191. The version of Java Mission Control I am using on my desktop is 6.0.0. Finally, the command I use to record my program's execution is the following:
java -XX:+UnlockCommercialFeatures -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,dumponexit=true,filename=test.jfr -classpath lib/*:src/ <my program with its arguments>
I should also add that connecting to the server directly with Java Mission Control is not an option (or is it?) as I am using an ssh rebound to actually connect to it ...
A liitle more research on my own provided me with the answer.
The JavaMonitorEnter events (and other events that one would like to monitor) need to be specified in a flight recorder configuration file. In this situation, I was using the profile configuration which is provided along with the default configuration with the Oracle JDK.
I created my own configuration using Java Mission Control. This blog was very helpful in presenting how to find the tool to create custom recording configurations in Java Mission Control.
I then exported my newly created configuration, uploaded it on my test environment and specified this configuration in my command (below the modified option only):
-XX:StartFlightRecording=settings=/home/<username>/Custom,<other options>...

How to run java program inside another java program [duplicate]

This question already has an answer here:
Running a java program from another java program
5 answers
I want to know how can I run a another java console application (jar1) inside another java application (jar2) also I want to be able to see jar1 outputs and insert my own inputs from jar2.
Let me explain more. there is a program called spigot (see more in Spigot is CLI java program for running a minecraft server
and you have to run it with launch flags in a bat file in windows server like:
java -Xmx 1024m -Xms 1024Xms spigot.jar1. Now I want to make a program which let me to running this on my own graphical design not in a console.
How can I pass arguments to an existing command window?
Run a new program with ProcessBuilder. You cannot modify arguments in any already running program, so terminate them first.

Command window for java?

Is there a way to just run individual java commands without having to create a file? Kind of like the matlab command window, where you can just type one line and it'll run.
Java is not a terminal programming language like matlab and python.
To run java applications, the commands must be compiled
When you compile, the java compiler turns the code into a class file. The class file contains "instructions" for the computer which is the application.
To clarify, Java 9 has JShell, which is a REPL for java. You can find the early access here.
I think what you are looking for is something like this
Sadly it isn't available until java 9, so in its current state it might not be production ready
try is here
this shows you how to get it going

Invoke java silently, Mac Os X

I have a huge generated shell script with a lot of lines like
java -jar <app_jar> <params>
Every invocation brings a java icon to the dock, making active application to loose the focus every line where java app is invoked
Is there a way to run it silently?
Java application is a single class, simple console app with a file IO routines, no windows created whatsoever
According to oracle's doc, the following should work:
java -Djava.awt.headless=true

Find and kill a specific Java process from another Java App

I have several java processes running on a windows machine. I have a Java process which is supposed to monitor the other processes and periodically kill or restart new ones.
If I have a java process running and one running - how can my monitoring process find and kill just the Main2 process?
Update: I have some code that can execute a command line tasklist.exe and parse it, but no matter what I do, I only see the java.exe process, not which class is executing
Update 2: I do not have the ability to install non-java programs.
It's probably going to be a lot simpler using OS-specific tools and using Runtime.exec() to run them, but I'll try and give a platform independent answer:
It might be possible to do this platform independently using the Attach API. This comes with the JDK, so to use it just include tools.jar from your JDK on your program's classpath.
To get a list of virtual machines on the system, use VirtualMachine.list(). You can get/parse arguments from the virtual machine descriptor objects that are returned from this.
The attach API also allows you to load agents into already-running Java processes. Since you want to kill a Java process, you can write a Java agent that simply runs System.exit() (or if you really want it dead use Runtime.halt() instead) when the agent loads.
Once you identify the one you want to kill, attach to it and load the killer agent (the agent has to be built as a JAR file, accessible to the Java process it needs to be loaded into). Shortly after the agent is attached that process should die.
These links might help also:
An Oracle blog on the attach API
Package documentation for java.lang.instrument (has detailed instructions on how to build an agent JAR)
This is specific to Windows.
I was facing the same issue where I have to kill the specific java program using taskkill. When I run the java program, tasklist was showing the same program with Image name set as java.exe. But killing it using taskkill /F java.exe will stop all other java applications other than intended one which is not required.
So I run the same java program using:
start "MyProgramName" java java-program..
Here start command will open a new window and run the java program with window's title set to MyProgramName.
Now to kil this java-program use the following taskkill command:
taskkill /fi "MyProgramName"
Your Java program will be killed only. Rest will be unaffected.