I have created a simple java networking program. I am using Fedora. whenever I want to see what the processes run on my system I found that for my application the process Name is java. I want give process name for my application. How to give process name.
Thanks
Sunil Kumar Sahoo
One way to change the process name of an application is to use a native launcher (or to copy the java/java.exe executable to another name).
Personally I've had good results with Launch4j
You could pass a java property to the jvm when you start the process then that should show up when running a ps -eaf and you could even do a ps -eaf|grep myprop to see if it's running.
so you start the app like this:
java -cp . com.whatever.MyApp -DMyAmazingProgram=true
then you should see the MyAmazingProgram=true in the ps output.
Another way would be to start your app from a bash script file e.g, startMyAmazingApp.sh then that should show up in the ps output until the process ends.
That script would have to not exit until the java process finished so you'd need to have a script a bit like this (rough guess):
#!/bin/bash
RESULT=`java -cp com.whatever.MyApp`
HTH
Related
So I have a Docker network that has a Docker file with a bunch of information. I have a java program that is going to bring up the enviorment and then produce several commands to run within this enviorment. To be clear, the first command I need to run is NOT inside the Docker enviorment. I am having some challenges with the Process and Runtime classes.
First, say I wanted my java program to launch a new gnome terminal and then run a command to get into the docker network. I have this command,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal"});
Gnome terminal sucessfully comes up but any additional arguments I give in this array are just ignored. For example,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","ls"});
Does not work. The command I ultimatly want to run would look something like this,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
Second, Once I have this running, will additional commmands I run work within the Docker enviorment? I have a python file with a Stream handler that specifies the correct commands to run.
Other documentation on related issues was limited.
I made sure my code was wrapped in a runtime exception try catch and that I was running the correct .class file. Any help on this would be great!
Edit: I have also tried to run this in another linux terminal like Hyper and Tilda
I also am able to get a sudo sign in when I run the command like so,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","--","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
However it closes immediatly after authorizing.
Okay this is what I was attempting to do.
https://www.atlassian.com/blog/software-teams/deploy-java-apps-with-docker-awesome
This site is outdated and I had to use this link for getting that latest version of the java PPA.
This process basically installs java into the docker contatiner so that I can run a java program that uses Runtime.
I am working on a KTor server. I run the generated jar file using java -jar command. So I expect that only one Java process should run. After running for a while another Java process is being created which is bound to different port.
I checked the details of the process using ps -a [PID] and find this new Java process is "kotlin-compiler-embeddable" program.
I am wondering why this process is being created, what is use of this and is it safe to kill it.
Thanks for any pointer.
kotlin-compiler-embeddable is used in scenarios when you have to package the compiler in a single jar, and no external dependencies. This is the case of Ktor.
Is there a way to start my Java process that I can easily identify it, from another program that runs a ps in the system?
You can set a dummy system property using the java command line. This will be easily visible in a ps.
java ..... -Djvm.identifier=xyz
java -Dvisualvm.display.name=wibble MyClass
Then you can find it with:
ps -ef | grep "visualvm.display.name=wibble"
If you use the visualvm.display.name property name then it will use that name when displaying your java processes in jvisualvm.
I want to kill the particular Java process in Windows, like in Linux (ps -aux to get processid and then kill processid to kill the process).
You can use the jps utility that is included in the JDK to find the process id of a Java process. The output will show you the name of the executable JAR file or the name of the main class.
Then use the Windows task manager to terminate the process. If you want to do it on the command line, use
TASKKILL /PID %PID%
You can also find the PID of a java program with the task manager. You enable the PID and Command Line columns View -> Select Columns and are then able to find the right process.
Your result will be something like this :
This will work even when there are multiple instance of jar is running
wmic Path win32_process Where "CommandLine Like '%yourname.jar%'" Call Terminate
After setting the path of your jdk use JPS.Then You can eaisly kill it by Task ManagerJPS will give you all java processes
The solution I found is very simple. Use Window's WMIC & Java's Runtime to locate & kill the process.
Part 1: You need to put some sort of identifier into your app's startup command line. E.g. something like:
String id = "com.domain.app";
Part 2: When you run your app, make sure to include the string. Let's say you start it from within Java, do the following:
Runtime.getRuntime().exec(
"C:\...\javaw.exe -cp ... -Dwhatever=" + id + " com.domain.app.Main"
);
Part 3: To kill the process, use Window's WMIC. Just make sure you app was started containing your id from above:
Runtime.getRuntime().exec(
"wmic process Where \"CommandLine Like '%" + id + "%'\" Call Terminate"
);
In windows, we can use the PowerShell to list the java running process. Then using the process id we can kill the process. Please find the below commands that needs to be executed in the PowerShell.
To list the Java Process.
ps | Where-Object -Property ProcessName -EQ -Value 'Java'
To kill the java process with specific id.
Stop-Process <PID>
The above approach worked for me.
In windows XP and later, there's a command: tasklist that lists all process id's.
For killing a process in Windows, see:
Really killing a process in Windows | Stack Overflow
You can execute OS-commands in Java by:
Runtime.getRuntime().exec("your command here");
If you need to handle the output of a command, see example: using Runtime.exec() in Java
This is specific to Windows.
I was facing the same issue where I have to kill one 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 kill this java-program use the following taskkill command:
taskkill /fi "MyProgramName"
Your Java program will be killed only. Rest will be unaffected.
Open Git Bash
Type ps -ef | grep java
Find the pid of running jdk
kill -9 [pid]
I have a JAVA application that launches (using ProcessBuilder) another JAVA application like this:
String val = "something";
ProcessBuilder processBuilder = new ProcessBuilder("java", "-classpath", dir, appName, val);
Process p = processBuilder.start();
Now, this works fine, appName is launched with the parameter val and it runs and works ... great ... the problem is no Console Window appears ... appName does a LOT of outputting to the console and we need to see it ... how can I start the process with a console?
I am trying stuff like ("CMD.exe", "java", "-classpath", dir, appName, val), etc... but I can't get it right ...
Also, I can't redirect the streams, my program can actually start 5-10 of these appName's, each should have their own console window showing their own information.
Any help would be much appreciated.
Thanks,
console windows are generally not the most reliable form of logging. they only store a set amount of information (buffer) and can behave differently across platforms.
i strongly suggest logging to a file using something like log4j and if you need to see it real time use a tail like program (i see you're using windows).
in addition to this, seeing as you want the windows visible at all times and launching a tail program for each log might be annoying, i'd write my own log window in java swing.
the basic idea is to not rely on the OS too much.
Tried Runtime.getRuntime().exec("cscript java -classpath ..."); ?
Anyway, consider using a logging framwork (log4j, commons-logging), because opening 5 consoles is not the most clever thing to do.
I call a few shell scripts via Process to open a command line window and launch whatever I need. As long as the scripts don't detach - you can usually stop any shell command from doing this -java will still hold the running process.
I did it in linux but the concept should be similar.
#!/bin/bash
# To open a process in a new window.
gnome-terminal -x ./your-real-shell-script-here.sh "$#"
the real script will have your java execution in it, such as:
#!/bin/bash
java -jar your-jar-file.jar "$#"
I think you can use javaw to run on windows, so you might only need the one shell script.
A Console object only exists when you execute java.... from a console. Otherwise, the call to obtain one returns null.
If you want to see a console, you need to open a command shell console (e.g. windows cmd.exe or Unix bash shell window) and type:
java -classpath="..." com.example.appName arg1
If you want to run in a different manner, sorry to say, logging to Console is not for you. Instead, log using one of:
log4j
slf4j
logback