-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?

Community
  • 1
  • 1
user2699067
  • 33
  • 2
  • 8
  • 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 – user2699067 Aug 01 '14 at 09:51
  • Can you provide a minimal example through a https://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. – Artjom B. Aug 01 '14 at 11:35

1 Answers1

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();
Community
  • 1
  • 1
sunny
  • 46
  • 4
  • 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. – user2699067 Aug 01 '14 at 09:45