Java代码来执行.sh文件

我有一个.sh文件存储在一些Linux系统。 该文件的完整path是:

 /comviva/CPP/Kokila/TransactionHandler/scripts/stopTH.sh 

我正在努力执行它

 Runtime.getRuntime().exec(`/comviva/CPP/Kokila/TransactionHandler/scripts/stopTH.sh`) 

但它是抛出一些例外。

我想在我的Java程序在MS-Windows环境中执行该文件; 可能吗?

要在Windows上执行.sh脚本,必须安装合适的命令解释程序。 例如,你可以在你的Windows机器上安装Cygwin环境,并使用它的bash解释器。

但是,即使使用Cygwin,Windows也不是Linux。 一些脚本将不会从一个环境移植到另一个环境而无需改动。 如果在Linux环境下通过Java执行脚本时遇到问题,我宁愿在该环境中调试该问题。

请记住,您可以在调试模式下在Linux上启动Java进程,并将Windows调试器连接到该远程进程。

这是我的代码。 正如评论所说,在Linux上工作,在Windows(XP)上失败。 AFAIK与Windows的问题是,cmd.exe是奇怪的参数。 对于您的特定子任务,您可能可以通过使用引号进行工作,也可以将子任务参数嵌入到子任务本身中。

 /** Execute an abritrary shell command. * returns the output as a String. * Works on Linux, fails on Windows, * not yet sure about OS X. */ public static String ExecuteCommand( final String Cmd ) { boolean DB = false ; if ( DB ) { Debug.Log( "*** Misc.ExecuteCommand() ***" ); Debug.Log( "--- Cmd", Cmd ); } String Output = ""; String ELabel = ""; String[] Command = new String[3]; if ( Misc.OSName().equals( "WINDOWS" ) ) { Command[0] = System.getenv( "ComSPec" ); Command[1] = "/C"; } else { Command[0] = "/bin/bash"; Command[1] = "-c"; } Command[2] = Cmd; if (DB ) { Debug.Log( "--- Command", Command ); } if ( Misc.OSName().equals( "WINDOWS" ) ) { Debug.Log( "This is WINDOWS; I give up" ); return ""; } try { ELabel = "new ProcessBuilder()"; ProcessBuilder pb = new ProcessBuilder( Command ); ELabel = "redirectErrorStream()"; pb.redirectErrorStream( true ); ELabel = "pb.start()"; Process p = pb.start(); ELabel = "p.getInputStream()"; InputStream pout = p.getInputStream(); ELabel = "p.waitFor()"; int ExitCode = p.waitFor(); int Avail; while ( true ) { ELabel = "pout.available()"; if ( pout.available() <= 0 ) { break; } ELabel = "pout.read()"; char inch = (char) pout.read(); Output = Output + inch; } ELabel = "pout.close()"; pout.close(); } catch ( Exception e ) { Debug.Log( ELabel, e ); } if ( DB ) { Debug.Log( "--- Misc.ExecuteCommand() finished" ); } return Output; } 

}

感谢大家的回应。 我找到了解决问题的办法。 对于这种情况,我们需要将我的Windows机器绑定到Linux系统。 这是工作的代码:

 public String executeSHFile(String Username, String Password,String Hostname) { String hostname = Hostname; String username = Username; String password = Password; try{ Connection conn = new Connection(hostname); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) throw new IOException("Authentication failed."); Session sess = conn.openSession(); sess.execCommand("sh //full path/file name.sh"); System.out.println("Here is some information about the remote host:"); InputStream stdout = new StreamGobbler(sess.getStdout()); BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); while (true) { String line = br.readLine(); if (line == null) break; current_time=line; System.out.println(line); } System.out.println("ExitCode: " + sess.getExitStatus()); /* Close this session */ sess.close(); /* Close the connection */ conn.close(); }catch(IOException e) { e.printStackTrace(System.err); System.exit(2); }finally{ } } 

谢谢。

我在这里做了一个快速的测试,假设你在你的机器上有/ bin / bash,下面的工作:

我的/tmp/test.sh:

 #!/bin/bash echo `ls` 

我的java代码:

 try { InputStream is = Runtime.getRuntime().exec("/bin/bash /tmp/test.sh").getInputStream(); int i = is.read(); while(i > 0) { System.out.print((char)i); i = is.read(); } } catch (IOException e) { e.printStackTrace(); } 

输出:当前目录中的所有文件

编辑:我有点忽略了“从Windows执行”的评论。 我不知道你的意思。

你可以创建一个.bat文件(批处理文件),可以在Windows上运行。 将.sh文件的内容放在.bat文件中,从你的应用程序启动一个进程:

 Process.Start("myFile.bat");