Home arrow static arrow Java Programming [Archive] - Getting Name of Java Application
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Getting Name of Java Application
This topic has 7 replies on 1 page.

Posts:4
Registered: 8/2/04
Getting Name of Java Application  
Aug 2, 2004 12:37 PM



 
Hi all.

How do I get the name of my Java JARred application at run time? I have searched far and wide on this site and tried the suggestions to no effect.

As the Java program runs under a *nix environment, I would like to know the program name so as to be able to terminate all spawned child processes. Obtaining the corresponding process ID would work as well. I realize that Java has no provisions to retrieve a process ID as this makes the application no longer platform indepedent, but I am not concerned about that.

Thanks.
 

Posts:2,206
Registered: 8/15/02
Re: Getting Name of Java Application  
Aug 2, 2004 12:39 PM (reply 1 of 7)



 
And whats wrong with ps
 

Posts:4
Registered: 8/2/04
Re: Getting Name of Java Application  
Aug 2, 2004 1:26 PM (reply 2 of 7)



 
Using ps to get the process ID would not work as many users may run the same application on the same system.
 

Posts:2,206
Registered: 8/15/02
Re: Getting Name of Java Application  
Aug 2, 2004 1:30 PM (reply 3 of 7)



 
Unless you are using some flags, ps only returns processes owned by the current user.
 

Posts:4
Registered: 8/2/04
Re: Getting Name of Java Application  
Aug 2, 2004 1:35 PM (reply 4 of 7)



 
I should have been more clear.

What I meant to say is in addition to many users running many instances of the same application, one user may also run many instances of the same application. Using ps in this case may return the process ID for the wrong instance for the particular user. What I would like is some mechanism to return the process ID at runtime for that particular instance despite the fact that the user may be running 10+ concurrent processes of the same application.
 

Posts:2,206
Registered: 8/15/02
Re: Getting Name of Java Application  
Aug 2, 2004 1:44 PM (reply 5 of 7)



 
Now that is more challenging. Have a look at this http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=34&t=005006
 

Posts:3,055
Registered: 18/06/98
Re: Getting Name of Java Application  
Aug 2, 2004 4:08 PM (reply 6 of 7)



 
You need to play with command-line parameters and ps -ef ( or -ax, depending on the version of Unix you`re using)

Say that you have a java app called as:

/usr/bin/java -jar MyLovelyApp arg1 arg2 arg3

and it spawns (due to threading and another issues) other processes that are described as

/usr/bin/java -jar MyLovelyApp arg1 arg2 arg3


ps -ef prints the executable name and some of the parameters (due to space restrictions it could not show all the info.)

You can check that the user id is yours in ps -ef output, and you have the PID and PPID (Parent PID).
You'll need to play with the user id, PID, PPID and some distinctive argument that you passed to the app.
 

Posts:4
Registered: 8/2/04
Re: Getting Name of Java Application  
Aug 2, 2004 4:51 PM (reply 7 of 7)



 
Thanks for all of your suggestions. I think I got it now.
 
This topic has 7 replies on 1 page.