java 进入sh文件_Java代码来执行.sh文件

要在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");

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值