Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question
    
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 1 '14 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. – Artjom B. Aug 1 '14 at 11:35
up vote 0 down vote accepted

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();
share|improve this answer
    
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 1 '14 at 9:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.