cutfilldemo/src/main/java/org/example/test/ExeMain.java

46 lines
1.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package org.example.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ExeMain {
public static void main(String[] args) {
//需要传入的参数ps: 多个参数用空格隔开)
// String paras = " --task C:\\Users\\Admin\\.cesiumlab\\tasks\\terrain_99254c80b1ef11ea8edfe728504fc83a.json --taskserver tcp://127.0.0.1:9001 --taskname 99254c80b1ef11ea8edfe728504fc83a --log_dir C:\\Users\\Admin\\.cesiumlab\\logs";
String paras = " -i E:\\code\\CutFillDemo\\target\\classes\\tmp.shp -o E:\\code\\CutFillDemo\\target\\classes\\tmp_dem.tiff -f ELEV -s 0.0000625";
//调用的exe可执行文件ps: 调用可执行文件和参数拼接必须要用空格隔开)
String cmd = "E:\\code\\contour2dem\\bin\\Debug\\contour2dem.exe" + paras;
openExe(cmd);
}
public static void openExe(String cmd) {
BufferedReader br = null;
BufferedReader brError = null;
try {
//执行exe cmd可以为字符串(exe存放路径)也可为数组调用exe时需要传入参数时可以传数组调用(参数有顺序要求)
Process p = Runtime.getRuntime().exec(cmd);
String line = null;
//获得子进程的输入流。
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
//获得子进程的错误流。
brError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = br.readLine()) != null || (line = brError.readLine()) != null) {
//输出exe输出的信息以及错误信息
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}