-2

How to execute phantom.exit() in java script from JAVA program.

Actually the scenario is Java program calls a java script program using PhantomJs, java script program is returning some values to that Java program. Refer to this link : Running Phantomjs from javascript, JSP or Java

After getting values from javascript I need to stop javascript. For that I used phantom.exit() in javascript, which is not executing when I call it from Java Program. But it works fine when I execute the same javascript program from command promt using the command 'phantomjs myscript.js'.

The code is getting stuck in this line.. int exitStatus = process.waitFor(); It keep on waiting and it is not terminating.

So what is the way to terminate javascript from JAVA program?

2
  • If you don't understand the question,please don't give negative votes.If this question doesn't make sense ,then before voting negative post proper explanation for that else don't vote negative Commented Aug 1, 2014 at 9:51
  • Can you provide a minimal example through a gist.github.com so that we can verify an try to fix it. It seems that the accepted answer didn't quite answer the question. So you don't have to accept it. Have you solved it yourself? If yes, you can provide an answer yourself. Commented Aug 1, 2014 at 11:35

1 Answer 1

0

From what I understand, you want to terminate Phantomjs after completing the required task through your Java code. Correct me if I am wrong..

To invoke Phantomjs from your java code, you can use Process. Here is an example similar to your problem.

Once you are done you can simply terminate the process using destroy() method.

So finally your code will look something like this..

String[] command = {...};
Process process = Runtime.getRuntime().exec(command);
//...
process.destroy();
Sign up to request clarification or add additional context in comments.

1 Comment

What you have answered is partially correct.Here I don't want to kill the process deliberately,in the sense the exitStatus should become 0 i.e.,Success and in your case exitStatus is 1.i.e.,process.waitFor() is not returning true.Please suggest a way to end the process successful.

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.