简体   繁体   中英

java Runtime.exec() crashes Netbeans

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!

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM