我有以下应该运行run.sh脚本的java代码。
public void executeScript() {
try {
Process p = null;
ProcessBuilder pb = new ProcessBuilder("phoneManagement/src/run.sh");
//pb.directory(new File("phoneManagement/src"));
p = pb.start(); // Start the process.
p.waitFor(); // Wait for the process to finish.
System.out.println("Script executed successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
run.sh看起来像这样:
#!/bin/sh
javac -cp ".:../../selenium/selenium-java-2.53.1.jar:../../selenium/selenium-server-standalone-2.53.1.jar:../../selenium/libs/*" phoneManagement.java
java -cp ".:../../selenium/selenium-java-2.53.1.jar:../../selenium/selenium-server-standalone-2.53.1.jar:../../selenium/libs/*" phoneManagement
如果我从命令行运行.sh脚本,它会执行它应该执行的操作,并且我的phoneManagement将被编译并执行。
当我使用我的executeScript类时,我得到消息"脚本成功执行"虽然run.sh没有做它应该做的事情。
任何人都可以帮我这个吗?
感谢名单
答案 0 :(得分:0)
你可以尝试:
Process builder pb = new ProcessBuilder ("bash", "-c", "/path/to/shell/script/run.sh");
答案 1 :(得分:0)
您必须指定脚本 public void jsonParser(String json) {
final String LOG_TAG = MainActivityFragment.class.getSimpleName();
// The variables that we want from the json file
final String title = "original_title"; // JSONObject
final String posterPath = "poster_path"; // JSONObject
final String synopsis = "overview"; // JSONObject
final String userRating = "popularity"; // JSONObject
final String releaseDate = "release_date"; // JSONObject
// Parsing the json file
try {
JSONArray jsonArray = new JSONArray(json);
// for is used to cycle through the json array.
topRatedMovieJson= new TopRatedMovieJson[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i); // getJSONObject(i) will take onw of the json array with i=0 as its first index
topRatedMovieJson[i] = new TopRatedMovieJson(
jsonObject.getString(title),
jsonObject.getString(posterPath),
jsonObject.getString(synopsis),
jsonObject.getString(userRating),
jsonObject.getString(releaseDate)
);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
的完整路径,或在开头"/foo/bar/script.sh"
使用带点的相对路径。另外,请不要忘记"./script.sh"
并在脚本开头添加适当的shebang chmod +x script.sh
。