簡體   English   中英

在java中調用shell腳本?

[英]Call a shell script in java?

是否可以在java類中調用shellcript或shellcode?

我的代碼(以靜態方法):

Runtime rtime = Runtime.getRuntime();
Process child = rtime.exec("/bin/bash");
BufferedWriter outCommand = new BufferedWriter(new
OutputStreamWriter(child.getOutputStream()));
outCommand.write("streamer -c /dev/video0 -b32 -o test.jpeg");
outCommand.flush();
outCommand.close();
child.destroy();

但是我得到這個錯誤,如果我在我的jsp頁面(tomcat6)中嘗試這個代碼:

java.security.AccessControlException:拒絕訪問(java.io.FilePermission / bin / bash execute)

解決這個問題的任何方法?

編輯 :ls -l / bin / bash向我展示了以下內容:

-rwxr-xr-x 1 root root 875596 2009-09-14 07:09 / bin / bash

謝謝

如果您希望保持安全管理器正常運行,並且您可能有充分的理由這樣做,那么您只需更改Tomcat正在使用的策略文件即可。 這些文件可能位於/var/lib/tomcat6/conf/policy.d

一個不錯的SO線程已經討論過了。 即使您將AllPermissions授予您的應用程序,使用SecurityManager運行也將允許您使用安全性有限的沙箱。 例如,您可能希望這樣做以運行可在運行時或類似情況下上載的JavaScript,或者您的應用程序中可能存在您由於某種原因而不信任的其他代碼。

嘗試禁用安全管理器。 編輯/etc/init.d/tomcat6並更改:

TOMCAT_SECURITY=yes

改為:

TOMCAT_SECURITY=no

然后重啟Tomcat:

/etc/init.d/tomcat6 restart

如果它有效,則由您來決定是否完全禁用安全管理器是否可接受(使用它實際上是不正常的IMO)。

如果可能,嘗試在單個exec調用中運行它。

Process p = Runtime.getRuntime().exec(args);

其中args是一個字符串數組的參數。

String[] args = new String[] {"/bin/bash", "streamer", "-c", "/dev/video0", "-b32", "-o", "test.jpeg" };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM