Chikage
Chikage

Reputation: 329

Find out if Java program is running from CMD or not

I want to write a Java program that receives input through text (a console program).
When this program starts up, straight away in the main method, I want to be able to check if the program was started from a Command Prompt (cmd) or from a JAR file. This is so that I can open a JFrame to receive input if there is no cmd to receive it.

Essentially, something like this:

public static void main(String[] args){
    if(startedFromCmd()) {
        startProgram();
    } else{
        openInputFrame();
        startProgram();
    }
}

Is there any built-in Java method which can check this?

Upvotes: 0

Views: 1605

Answers (3)

mikera
mikera

Reputation: 106401

Ben's answer is correct for old Java versions but there is an update for recent Java versions (22+ I believe).

Recent Java versions do return a Console even if not run from the console, so you need to check the isTerminal method in this case. You should be able to use something like the following code to do this:

    public static boolean checkIfTerminal() {
        Console c=System.console();
        // If null, we have no terminal (Java up to 21)
        if (c==null) return false;
        
        // We have a console, but in Java 22+ we need to check if it is actually a terminal
        try {
            Method m=c.getClass().getMethod("isTerminal");
            return  (Boolean)m.invoke(c);
        } catch (NoSuchMethodException e) {
            return true;
        } catch (SecurityException | IllegalAccessException | InvocationTargetException e) {
            // Shouldn't happen?
            return false;
        } 
    }

Upvotes: 0

Sabesh
Sabesh

Reputation: 320

You can get the CMD process that are running currently by using WMIC command. However, this works on windows only. For unix/Mac you can use something similar to ps -ef|grep java.
wmic process where "name like '%java%'" get commandline,processid

Above command will fetch you the list of java process in which you can filter out your file. Please find a sample code here

Upvotes: 0

Ben
Ben

Reputation: 1695

You can use System.console() which returns a console object if there is one attached to the Java Virtual Machine.

The method returns null if there is no console attached. Note that this is not 100% reliable. E.g. the method returns null for the internal console in the Eclipse IDE (which is actually bug #122429). It works with the Windows command line though.

An example could look like this:

public static void main(String[] args)
{
    if (System.console() != null)
    {
        System.out.println("There is a console!");
    }
    else
    {
        JFrame frame = new JFrame("There is no console!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(100, 100);
        frame.setVisible(true);
    }
}

Running this in the Windows command line with java -jar MyJar.jar will result in

There is a console!

being printed on the console.

Double clicking the .jar-file will show a JFrame.

Upvotes: 2

Related Questions