user2889419
user2889419

Reputation:

How to findout main method has called by JVM or existing process?

Considering the following code

public static void main(String...arg){
//do something
if(<<the method has called by a new process>>){System.exit(0);}
else{System.setProperty("main_result","0");return;}
}

the main method would be called by a separated process by JVM or existing process, now how can I find it out?
Thanks in advance

Upvotes: 0

Views: 61

Answers (5)

Sergey Fedorov
Sergey Fedorov

Reputation: 2169

It would be better to consider refactoring and get rid of such problem.

Otherwise the following code can help:

    if(Thread.currentThread().getStackTrace()[1].getClassName().equals(
            System.getProperty("sun.java.command"))){
        System.out.println("!");
    }

Will not work if there is no "sun.java.command" property (on not Sun/Oracle JVMs it may absent)

Upvotes: 0

Ian Roberts
Ian Roberts

Reputation: 122364

I would refactor the code as follows:

public static void main(String...arg){
  System.exit(doStuff(arg));
}

public static int doStuff(String... arg) {
  //do something
}

To access this logic within the same JVM you can now call MyClass.doStuff and get the return value directly.

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109547

Let's clarify: there might be another class with a main that was started, or the main is somehow called again.

Normally you want to call System.exit(0) (or return;?) but when called from the program itself you want to end in System.setProperty("main_result","0");.

public static void otherMain(String[] args) {  
    Main.main(args);
}

public static void main(String[] args) {  

    ...

    StackTraceElement[] elems = Thread.currentThread().getStackTrace();
    //for (StackTraceElement elem : elems) {
    //    System.out.printf("%s%n", elem.getClassName());
    //}
    if (elems.length > 2) { // [0] Thread [1] main
        System.setProperty("main_result","0");
    }
}

Upvotes: 1

Robert Balent
Robert Balent

Reputation: 1462

Do you really need it? Just don't use System.exit(0); and refactor main method to finish gracefully.

Calling System.setProperty in both cases - when run as new process and also as a class on classpath, will not make any difference.

Edit: Finding out who is calling the method is not easy and definitely bad practice.

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

In java, every Java process runs in its own JVM. So, the "same" main method cannot be called by a different process under normal circumstances

Even if you run the same program twice, they will be running in their own JVMs.

You can try one thing.. Keep a static variable in your program, run it and make it sleep for a long period of time (process 1).. Now, run the same program again and update the static variable(runs in process 2).. See, whether it will be updated in the first process (No, it won't be updated as each process will have it's own set of variables..)

Upvotes: 1

Related Questions