0

I use the following to launch a Java application from another Java app.

    ProcessBuilder pb = new ProcessBuilder(javaPath + javaCommand, maxMemStr,
            minMemStr, stackSizeStr, jarCommand, jarfile, jarArg);
    try {
        Process p = pb.start();
    } catch (IOException ex) {
        Logger.getLogger(launch.class.getName()).log(Level.SEVERE, null, ex);
    }

where javaCommand is either java or javaw (javaPath is empty most of the time unless a user points to an alternate path). The problem is, after the app launches, even when I verify the process list to contain java, it doesn't show the console.

Is it because PrcoessBuilder doesn't invoke the command shell? Is there a way to show the console programatically?

Thanks in advance.

2
  • Possible duplicate of this SO question? Commented Jan 22, 2013 at 19:24
  • Ah, my SO-foo is weak today. Though, I must say, the answers here were more relevant :) Commented Jan 22, 2013 at 19:50

3 Answers 3

1

This is because the "command console" itself is a process that attaches to the std-in/-out/-err streams of another process and displays them on the screen. When you launch Java all by itself, no other processes will be handling those streams, hence the lack of a command console. To get the results you want, you will need to launch a new instance of the command console and subsequently have it run your custom java command.

There may be a better way to do this... but I think the solution to this is going to be platform-dependent. In Windows, you could do something like:

ProcessBuilder pb = new ProcessBuilder("start", "\"JAwesomeSauce\"", "cmd.exe",
    "/k", javaPath + javaCommand, maxMemStr, minMemStr, stackSizeStr, jarCommand,
    jarfile, jarArg);
try {
    Process p = pb.start();
} catch (IOException ex) {
    Logger.getLogger(launch.class.getName()).log(Level.SEVERE, null, ex);
}

I assume you could do something similar in Linux/Mac if that's the O/S you're using.

Sign up to request clarification or add additional context in comments.

2 Comments

I was hoping to find a system agnostic solution instead of applying platform specific handling. Is there another way to launch the console apart from getting System.console()? Because in that case, I believe it will return null in my context.
Yeah, I hear ya... unfortunately, Java's "Platform Independence" is somewhat exaggerated... ;) System.console() returns a console implementation for the JVM you're currently running in. I could be wrong, but I don't believe you can get an actual console object for a separate process, unless you get the input/output/error streams from the Process object you create - but at that point, you'd have to also write your own "Console" implementation because java.io.Console doesn't let you modify its streams.
1

You may want to run the command like this: cmd /K java ... or cmd /C java ...

1 Comment

thanks jdb. I'll definitely try this out and look for its counterparts in other OSes.
1

As far as I remember the Processbuilder opens a pipe to a specific process.

Your command window is a process itself with all you see. If you enter commands the cmd/bash usually creates new processes and attaches to them.

2 Comments

thank you Stefan. your advice is similar to what the others here are offering. I appreciate it.
yeah, just wanted to through in the pipe keyword for better understanding although all other answers are in their own way all correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.