java bash命令问题

发布于 2024-11-05 02:38:31 字数 1530 浏览 1 评论 0原文

可能的重复:
如何从 Java 内运行的进程获取 bash 命令退出代码?

嗨,我有一个程序,它是一个简单的使用 bash 命令解密另一个文件的程序 ./nv0914 ./nv0914 ./nv0914 nv0914.challenge 其中nv0914是unix可执行文件,nv0914.challenge是用于解密的文本文件。现在,当我从 bash 运行它时,如果我运行命令 echo ${?},我会得到一个值 0,这很好。但现在我必须实施攻击,为此我需要通过 java 程序打印退出值。我现在有这个代码:

import java.io.*;
   import java.util.*;

   public class ExecBashCommand {
     public static void main(String args[]) throws Exception {


       Runtime runtime = Runtime.getRuntime();
       Process process = runtime.exec("./nv0914 < nv0914.challenge");

      /* InputStream is = process.getInputStream();
       InputStreamReader isr = new InputStreamReader(is);
       BufferedReader br = new BufferedReader(isr);
       String line;*/

System.out.println("Exit Code: "+process.exitValue()); 
       //System.out.printf("Output of running %s is:", Arrays.toString(args));

      /* while ((line = br.readLine()) != null) {
         System.out.println(line);
       }*/

     }
    } 

但是这个程序给了我一个错误:

Exception in thread "main" java.lang.IllegalThreadStateException: process hasn't exited
        at java.lang.UNIXProcess.exitValue(UNIXProcess.java:172)
        at ExecBashCommand.main(ExecBashCommand.java:16)

有关如何获取退出值的任何建议。谢谢

Possible Duplicate:
How do I get the bash command exit code from a Process run from within Java?

Hi, I have a program which is a simple decrypts another file using bash command ./nv0914 < nv0914.challenge where nv0914 is a unix executable file and nv0914.challenge is a text file used for decryption. Now when i am running it from bash and just after that if I run the command echo ${?} I am getting a value 0, which is good. But now I have to implement an attack and for that I need to get the exit value printed through a java program. I have this code as if now:

import java.io.*;
   import java.util.*;

   public class ExecBashCommand {
     public static void main(String args[]) throws Exception {


       Runtime runtime = Runtime.getRuntime();
       Process process = runtime.exec("./nv0914 < nv0914.challenge");

      /* InputStream is = process.getInputStream();
       InputStreamReader isr = new InputStreamReader(is);
       BufferedReader br = new BufferedReader(isr);
       String line;*/

System.out.println("Exit Code: "+process.exitValue()); 
       //System.out.printf("Output of running %s is:", Arrays.toString(args));

      /* while ((line = br.readLine()) != null) {
         System.out.println(line);
       }*/

     }
    } 

But this program is giving me an error :

Exception in thread "main" java.lang.IllegalThreadStateException: process hasn't exited
        at java.lang.UNIXProcess.exitValue(UNIXProcess.java:172)
        at ExecBashCommand.main(ExecBashCommand.java:16)

Any suggestions as to how to get the exit value. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2024-11-12 02:38:31

在尝试读取退出值之前,您是否尝试过调用 process.waitFor() ?

Have you tried calling process.waitFor() before you try and read the exit value?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文