Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Spawning jvm processes  (Read 1423 times)
0 Members and 1 Guest are viewing this topic.
Offline immudium

Junior Member




Gorram it!


« Posted 2004-02-20 18:37:24 »

This is kind of an experiment on my part and not absolutely critical if it can't be done easily, but I'm wondering if it's possible to launch separate jvm processes from an already existing jvm process.  The reason I want to do this is to be able to compare the behavior and performance of certain global settings side by side.  For example I run my "launcher" app, set my settings such as sun.java2d.noddraw true or false, click a button and then launch the new window with the approprate settings enabled.  Then I can launch as many processes as I want observe their behavior/appearance and then clean them all up by just closing the launcher/master application.

Anway, can this be done or is it more trouble than it's worth.  Thanks for any help.
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #1 - Posted 2004-02-20 18:52:13 »

Have a look at java.lang.Runtime, especially:

1  
2  
exec(String command)
          Executes the specified string command in a separate process.


and friends.

malloc will be first against the wall when the revolution comes...
Offline Mark Thornton

Senior Member





« Reply #2 - Posted 2004-02-20 19:51:41 »

Quote
clean them all up by just closing the launcher/master application.

Windows doesn't (usually) maintain a parent/child relationship between processes. This means that closing the parent does not close the children. You could register a shutdown task in the parent and explicitly kill any remaining children during the parent process shutdown. On Unix/Linux of course killing the parent does kill all its children.

Number 2 on the bug parade:
http://developer.java.sun.com/developer/bugParade/bugs/4109888.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline immudium

Junior Member




Gorram it!


« Reply #3 - Posted 2004-02-21 00:10:54 »

Thank You the information so far has been very helpful.  I have a couple of questions, however, that I haven't been able to sort out.  For instance, the java docs state for Runtime.exec():
Quote
If envp is null, the subprocess inherits the environment settings of the current process

However, I am unable to successfully launch the new process without explicitly settings the classpath in the environment argument.  Shouldn't it be able to figure it out from the current process as it states?

Second, I can't seem to register a shutdown task through addShutdownHook.  Is this not the correct function to use?

I wrote a small test program.  Is there something I'm doing wrong?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
package com.launcher;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class MainLauncher extends JFrame
{
      private JButton launchTest1;
      private JButton launchTest2;
      private JPanel buttonPanel;
     
      private ArrayList externalProcessList = new ArrayList();
     
      private String [] envp = new String [] {"CLASSPATH=c:\\projects\\LauncherTest\\bin"};
      public MainLauncher(String title)
      {
            super(title);
           
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           
            initComponents();
           
            Runtime.getRuntime().addShutdownHook(new ShutdownThread());
      }
     
      public void initComponents()
      {
            buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(1, 2, 2, 2));
           
            launchTest1 = new JButton("Test 1");
            launchTest1.addActionListener(new ActionListener()
            {
                  public void actionPerformed(ActionEvent e)
                  {
                        try
                        {
                              //Does not work.  Returns a main class not found exception.
                             externalProcessList.add(Runtime.getRuntime().exec("javaw com.space.TestFrame -1"));
                        }
                        catch (IOException e1)
                        {
                              e1.printStackTrace();
                        }
                  }      
            });
           
            launchTest2 = new JButton("Test 2");
            launchTest2.addActionListener(new ActionListener()
            {
                  public void actionPerformed(ActionEvent e)
                  {
                        try
                        {
                              //Correctly launches the separate process but does not get shutdown through the registered Shutdown thread :(
                             externalProcessList.add(Runtime.getRuntime().exec("javaw -Dsun.java2d.noddraw=true com.space.TestFrame -2", envp));
                        }
                        catch (IOException e1)
                        {
                              e1.printStackTrace();
                        }
                  }      
            });
           
            buttonPanel.add(launchTest1);
            buttonPanel.add(launchTest2);
           
            getContentPane().add(buttonPanel, BorderLayout.NORTH);
      }
     
      private class ShutdownThread extends Thread
      {      
            public void run()
            {
                  System.out.println("Shutting down external processes");
                  Iterator iterator = externalProcessList.iterator();
                  while(iterator.hasNext())
                  {
                        Process process = (Process)iterator.next();
                        process.destroy();
                  }
                  System.out.println("Finished shutting down external processes");
            }
      }
     
      public static void main(String[] args)
      {
            MainLauncher launcher = new MainLauncher("Launcher Test Suite");
            launcher.show();
            launcher.pack();
      }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
package com.space;

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JTextArea;


public class TestFrame extends JFrame
{
     
      public TestFrame(int frameID)
      {
            super("Test Frame" + frameID);
           
            initComponents();
      }
     
      public void initComponents()
      {
            setFont(new Font("Arial", 0, 25));
           
            getContentPane().add(new JTextArea("This window was launched in it's own process"), BorderLayout.NORTH);
      }
     
      public static void main(String[] args)
      {
            int frameID = Integer.parseInt(args[0]);
            TestFrame frame = new TestFrame(frameID);
            frame.show();
            frame.pack();
      }
}
Offline Mark Thornton

Senior Member





« Reply #4 - Posted 2004-02-22 16:11:18 »

Quote

Second, I can't seem to register a shutdown task through addShutdownHook.  Is this not the correct function to use?

What goes wrong when you try? Note that shutdown hooks will only be run if the process is terminated nicely. Killing the process from Task manager does not give the JVM an opportunity to do any cleanup. The same applies if you kill it from an IDE. If the process is run using java.exe from a command prompt then Ctrl-C will allow the cleanup to take place. Similarly if you call System.exit() from within the process.

As to the environment, I just use the exec variants which don't require passing it and don't have any problem.
Offline Jeff

JGO Coder




Got any cats?


« Reply #5 - Posted 2004-02-22 17:55:32 »

Quote
Thank You the information so far has been very helpful. For instance, the java docs state for Runtime.exec():
However, I am unable to successfully launch the new process without explicitly settings the classpath in the environment argument.  Shouldn't it be able to figure it out from the current process as it states?


Not if its not in your shell ENVIRONMENT variables.

For instance if you are using -classpath on your command line.



Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline immudium

Junior Member




Gorram it!


« Reply #6 - Posted 2004-02-22 21:57:42 »

Quote
Note that shutdown hooks will only be run if the process is terminated nicely

D'oh! That's it.  For some reason, I had it in my head that terminating it from my IDE was like hitting Ctrl-C from cmd, but I can see why that wouldn't be the case and goes with what you said before about Windows not killing child processes.

Quote
Not if its not in your shell ENVIRONMENT variables


Makes sense.  I guess it's not really that big of a deal to set an explicit path for this, but since the type A side of me needs to be appeased, I found that
1  
private String [] envp = new String [] {"CLASSPATH=" + System.getProperty("java.class.path")};
seems to work pretty well for getting the path to wherever it was launched from.

As always, thanks for the help  Smiley
Offline Mark Thornton

Senior Member





« Reply #7 - Posted 2004-02-23 19:52:50 »

Quote

Note that shutdown hooks will only be run if the process is terminated nicely.


Unfortunately it seems the Sun JVM does not process either the WM_ENDSESSION or WM_QUIT messages correctly. As result shutdown processing is unreliable for processes with a message loop (anything gui). Even worse the Sun engineers do not seem to understand the issue. (see bugs 4486580 and 4302814).
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (16 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!