0

I'm trying to execute julia.exe in Java.

Here is the code:

Process pTest = Runtime.getRuntime().exec("C:/Program Files/Julia-0.4.1/bin/julia.exe");

When I run it, nothing happens.

However, if I try another executable file, it works well. For example:

Process pTest = Runtime.getRuntime().exec("C:/Program Files/anotherProgram/program.exe");

program.exe will run just as expected.

julia.exe is a little special.
If I run it on command prompt, it will execute on the command prompt. In other words, it won't pop up its own window.

I've done a test:

#julia script, it's path: C:/Users/Thomas/Julia/test.jl
function test1()
  println("it's test1")
end

test1()

I execute this command on the command prompt:

C:\>C:/Program Files/Julia-0.4.1/bin/julia.exe C:/Users/Thomas/Julia/test.jl

then I will get it's test1 on the command prompt.

What I need is to execute C:/Program Files/Julia-0.4.1/bin/julia.exe C:/Users/Thomas/Julia/test.jl in my java project and get it's test1 on the console of eclipse.

Here is my java project:

public class Main {
    public static void main(String[] args){
        try {

        String[] params = {"C:/Program Files/Julia-0.4.1/bin/julia.exe", "C:/Users/Thomas/Julia/test.jl"};

        Process pTest = Runtime.getRuntime().exec(params);
        try {
            if (pTest.waitFor() != 0) {
                System.err.println("exit value = " + pTest.exitValue());
            }
            BufferedReader in = new BufferedReader(new InputStreamReader(pTest.getInputStream()));
            StringBuffer stringBuffer = new StringBuffer();
            String line = null;
            while ((line = in.readLine()) != null) {
                stringBuffer.append(line+"-");
            }
            System.out.println(stringBuffer.toString());
        } catch (InterruptedException e) {
            System.err.println(e);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

Yves
  • 11,597
  • 17
  • 83
  • 180
  • 1
    Does it work when you run it on command prompt? – Rao Nov 26 '15 at 12:55
  • 2
    1. What is the program supposed to do? 2. Do you handle the process's streams appropriately? – Hovercraft Full Of Eels Nov 26 '15 at 12:56
  • Do you have admin privileges to the location of executable? – Naman Nov 26 '15 at 12:57
  • To expand on @Rao - does your program work from the command prompt? i.e., if you use "java -jar whatever.jar" to run your program, does it work; if it doesn't, it is liable to give stacktrace info that is useful. – arcy Nov 26 '15 at 12:57
  • I'd advise against hardcoding the directory "slashes". Use java.io.File.separator instead. – Bathsheba Nov 26 '15 at 12:59
  • @Rao if I run `julia.exe` on command prompt, it will execute itself on the command prompt. It won't pop up its own window. – Yves Nov 26 '15 at 13:00
  • Ok, then how would you confirm it is not working when you run with your program? Hmm you would want to try `Runtime.getRuntime().exec("cmd /c C:/Program Files/Julia-0.4.1/bin/julia.exe");` – Rao Nov 26 '15 at 13:01
  • @nullpointer yes I have it – Yves Nov 26 '15 at 13:01
  • Did you try to run julia.exe from other directory? Does it work? – Krzysiek Nov 26 '15 at 13:02
  • 1
    When "nothing happens", the thing that doesn't actually happen is a failure. So something is working, or you'd get an error. The question becomes: what are you expecting to happen? If you think that invoking exec() is going to produce console output just like that, you're wrong. – Gimby Nov 26 '15 at 13:04
  • @Rao I reedit my question. – Yves Nov 26 '15 at 13:06
  • `Runtime.getRuntime().exec("C:/Program Files/Julia-0.4.1/bin/julia.exe C:/Users/Thomas/Julia/test.jl")` should give you something going on console – Naman Nov 26 '15 at 13:09
  • @nullpointer but I get nothing. – Yves Nov 26 '15 at 13:10
  • @Jan yes, I just add my whole java project. See my newest question. – Yves Nov 26 '15 at 13:10
  • @Thomas : did you look into [this] (http://stackoverflow.com/questions/22240581/running-julia-jl-files) ? – Naman Nov 26 '15 at 13:15
  • 2
    Two things: 1: you .waitFor() the julia process to finish... **before** you start and capture the output... ?! 2: I don't get why you Buffer the output from julia before you `System.out.println` that - you could "print as you go". Also waiting for complete lines might be blocking? – Jan Nov 26 '15 at 13:17
  • @Thomas, this must help you to resolve http://stackoverflow.com/questions/5711084/java-runtime-getruntime-getting-output-from-executing-a-command-line-program – Rao Nov 26 '15 at 13:18
  • You're blocking everything with the `waitFor()` and `exitValue()` call -- meaning no streams will be handled. – Hovercraft Full Of Eels Nov 26 '15 at 13:26

2 Answers2

1

Consider this changed (and working) implementation removing the too-early invocation of waitFor and exitValue:

public class Main {
  public static void main(String[] args) {
    try {

        String[] params = {"C:/Program Files/Julia-0.4.1/bin/julia.exe", 
              "C:/Users/Thomas/Julia/test.jl"};

        Process pTest = Runtime.getRuntime().exec(params);
        BufferedReader in = new BufferedReader(new InputStreamReader(pTest.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        System.out.println("===");
        System.out.println("Julia exit value = " + pTest.exitValue());

    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}

This produced the following output with your test-script:

it's test1
===
Julia exit value = 0
Jan
  • 13,738
  • 3
  • 30
  • 55
0

I got it finally.

As julia.exe execute on the command prompt immediately, we must give admin privileges to the users of cmd.exe.

Yves
  • 11,597
  • 17
  • 83
  • 180