0

I have been searching the web for quite some time now and I did find a lot about Runtime.exec(), but I didn't find a satisfying answer to my problem yet, so I decided to open a new question.

I am running asymptote (http://asymptote.sourceforge.net/) from java. After reading some articles and tinkering around a bit I found this (working) solution:

public static voidcompileAsy(File name)
{
    try
    {
        Runtime rt = Runtime.getRuntime();           
        String[] cmdarray = {"/usr/texbin/asy", name.getName()};
        //String[] envp = {"PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/texbin:/usr/local/bin"};
        String[] envp = null;
        File fd = new File("/xxxxxx/xxxxx");
        Process proc = rt.exec(cmdarray, envp, fd);

        // any errors?
        StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "error");
        // any output?
        StreamGobbler outputGobbler = new  StreamGobbler(proc.getInputStream(), "output");        
        // kick them off
        errorGobbler.start();
        outputGobbler.start();            

        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);            
    }
    catch(Throwable t)
    {
        t.printStackTrace();
    }   

So far so good. It turns out that without the correct path variables set asymptote crashes, which would be no problem if I could catch this event from the java side. Unfortunately when asymptote crashes it takes down java entirely including Netbeans so I have no chance for any diagnosis. Here are my questions:

  • How does this happen? Isn't asymptote a process on its own and should die without touching the jvm?
  • How can I prevent this from happening?

The system is MacOSX 10.10.3

Happy to hear any opinion/suggestions on this!

5
  • Do you have any more info on the crash? Dumps, etc? Commented May 15, 2015 at 19:01
  • I did simulate the situation with a bash with an empty PATH variable and got the following:/usr/texbin/asy Cannot execute kpsewhich Terminated: 15 Commented May 16, 2015 at 9:38
  • Also tried to attach a shutdown hook to write a stack-trace to a file (described here stackoverflow.com/questions/10714828/…), but could not generate a stack-trace out of it either... Commented May 16, 2015 at 10:36
  • Assuming your StreamGobbler starts a thread you should probably still do something to join those threads to the current thread. Commented May 17, 2015 at 8:04
  • Well my StreamGobbler is a copy of this one: javaworld.com/article/2071275/core-java/… How should I change it to avoid the connected process crashing the JVM? Commented May 20, 2015 at 6:30

1 Answer 1

1

There is one thing I can see that is wrong with your code above and that is that you read the error stream and then read the input stream.

This can cause execution to block and stream buffers fill up.

You should create a separate thread for each stream and once your waitfor call has completed join the threads. I do not know if this is contributing to the crash in some way. Perhaps you are getting an input stream buffer overflow.

Consider changing to use ProcessBuilder, which has simpler options for handling process outputs like inheritIO()

Sign up to request clarification or add additional context in comments.

1 Comment

Thats ok. Fixed it already. As I told I know now that the environment variables were the culprit. I already exchanged it in my code by a stream-gobbler like aproach. What I am looking for is an explanation, why I can not catch the crash of the process I am generating.

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.