在 Java 中执行命令 julia.exe
Execute the command julia.exe in Java
我正在尝试在 Java 中执行 julia.exe
。
代码如下:
Process pTest = Runtime.getRuntime().exec("C:/Program Files/Julia-0.4.1/bin/julia.exe");
当我运行它时,没有任何反应。
但是,如果我尝试另一个可执行文件,它运行良好。例如:
Process pTest = Runtime.getRuntime().exec("C:/Program Files/anotherProgram/program.exe");
program.exe
会 运行 正如预期的那样。
julia.exe
有点特别
如果我在命令提示符下 运行 它,它将在命令提示符下执行。换句话说,它不会弹出自己的 window.
我做过测试:
#julia script, it's path: C:/Users/Thomas/Julia/test.jl
function test1()
println("it's test1")
end
test1()
我在命令提示符下执行这条命令:
C:\>C:/Program Files/Julia-0.4.1/bin/julia.exe C:/Users/Thomas/Julia/test.jl
然后我会在命令提示符上得到 it's test1
。
我需要的是在我的java项目中执行C:/Program Files/Julia-0.4.1/bin/julia.exe C:/Users/Thomas/Julia/test.jl
,然后在eclipse的控制台上得到it's test1
。
这是我的 java 项目:
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();
}
}
}
考虑这个改变的(和有效的)实现删除了 waitFor
和 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();
}
}
}
这使用您的测试脚本产生了以下输出:
it's test1
===
Julia exit value = 0
终于明白了
由于 julia.exe
立即在命令提示符下执行,我们必须将管理员权限授予 cmd.exe
的用户。
我正在尝试在 Java 中执行 julia.exe
。
代码如下:
Process pTest = Runtime.getRuntime().exec("C:/Program Files/Julia-0.4.1/bin/julia.exe");
当我运行它时,没有任何反应。
但是,如果我尝试另一个可执行文件,它运行良好。例如:
Process pTest = Runtime.getRuntime().exec("C:/Program Files/anotherProgram/program.exe");
program.exe
会 运行 正如预期的那样。
julia.exe
有点特别
如果我在命令提示符下 运行 它,它将在命令提示符下执行。换句话说,它不会弹出自己的 window.
我做过测试:
#julia script, it's path: C:/Users/Thomas/Julia/test.jl
function test1()
println("it's test1")
end
test1()
我在命令提示符下执行这条命令:
C:\>C:/Program Files/Julia-0.4.1/bin/julia.exe C:/Users/Thomas/Julia/test.jl
然后我会在命令提示符上得到 it's test1
。
我需要的是在我的java项目中执行C:/Program Files/Julia-0.4.1/bin/julia.exe C:/Users/Thomas/Julia/test.jl
,然后在eclipse的控制台上得到it's test1
。
这是我的 java 项目:
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();
}
}
}
考虑这个改变的(和有效的)实现删除了 waitFor
和 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();
}
}
}
这使用您的测试脚本产生了以下输出:
it's test1
===
Julia exit value = 0
终于明白了
由于 julia.exe
立即在命令提示符下执行,我们必须将管理员权限授予 cmd.exe
的用户。