请澄清下面java代码中的问题

发布于 2024-09-12 12:02:03 字数 1163 浏览 4 评论 0 原文

  import java.lang.Process;    
  import java.io.*;   
  import java.io.InputStream;    
  import java.io.IOException;    
  public class prgms{    
  public static void main(String[] args) {    
    try {    
      // Execute a command without arguments    
      String command = "java JavaSimpleDateFormatExample";    
      Process child = Runtime.getRuntime().exec(command);    

      // Execute a command with an argument    
      // command = "java JavaStringBufferAppendExample";    
     //child = Runtime.getRuntime().exec(command);    
    } catch (IOException e) {    
    }    
    InputStream in = child.getInputStream();    
    int c;    
    while ((c = in.read()) != -1) {    
        process((char)c);    
    }    
    in.close();    
  }    
}    

我已经这样修改了...但是出现以下错误,

prgms.java:17: cannot find symbol    
symbol  : variable child    
location: class prgms     
InputStream in = child.getInputStream();    
                 ^
prgms.java:20: cannot find symbol    
symbol  : method process(char)    
location: class prgms    
        process((char)c);    
        ^    
2 errors   
  import java.lang.Process;    
  import java.io.*;   
  import java.io.InputStream;    
  import java.io.IOException;    
  public class prgms{    
  public static void main(String[] args) {    
    try {    
      // Execute a command without arguments    
      String command = "java JavaSimpleDateFormatExample";    
      Process child = Runtime.getRuntime().exec(command);    

      // Execute a command with an argument    
      // command = "java JavaStringBufferAppendExample";    
     //child = Runtime.getRuntime().exec(command);    
    } catch (IOException e) {    
    }    
    InputStream in = child.getInputStream();    
    int c;    
    while ((c = in.read()) != -1) {    
        process((char)c);    
    }    
    in.close();    
  }    
}    

I have modified this way... but the following error occurs,

prgms.java:17: cannot find symbol    
symbol  : variable child    
location: class prgms     
InputStream in = child.getInputStream();    
                 ^
prgms.java:20: cannot find symbol    
symbol  : method process(char)    
location: class prgms    
        process((char)c);    
        ^    
2 errors   

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

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

发布评论

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

评论(3

自由如风 2024-09-19 12:02:03

您确实忽略了 stdout Process 的 stderrRuntime#exec()

这将是一个很长的故事,所以这里只是一个链接: 当 Runtime.exec 不会时。阅读全部四页。

You're indeed ignoring the stdout and stderr streams of the Process returned by Runtime#exec().

This is going to be a long story, so here's just a link: When Runtime.exec won't. Read all the four pages.

淡笑忘祈一世凡恋 2024-09-19 12:02:03

该代码没有问题。

它的作用是在里面执行另一个Java程序。

Process 类有一个方法来获取程序的输出,如果您想查看结果,您必须将该输出重定向到您自己的输出。

以下是使用 Runtime.exec 输出的“现代”替代方案的示例

// Hello.java says Hello to the argument received.
class Hello {
    public static void main ( String [] args ) {
         System.out.println( "Hello, "+args[ 0 ] );
    }
}

// CallHello.java 
// Invokes Hello from within this java program 
// passing "world"  as argument.
import java.io.InputStream;
import java.io.IOException;
public class CallHello {
    public static void main( String [] args ) throws IOException {
         Process child = new ProcessBuilder("java", "Hello", "world").start(); 
         // read byte by byte the output of that progam.
         InputStream in = child.getInputStream();
         int c = 0;
         while( ( c = in.read() ) != -1 ) {
            // and print it
            System.out.print( (char)c);
         }
     }
}

Hello world

There is no problem with that code.

What is does, is to execute another Java program inside.

The class Process has a method to get the output of the program, you have to redirect that output to your own, if you want to see the result.

Here's a sample using a "modern" alternative to Runtime.exec

// Hello.java says Hello to the argument received.
class Hello {
    public static void main ( String [] args ) {
         System.out.println( "Hello, "+args[ 0 ] );
    }
}

// CallHello.java 
// Invokes Hello from within this java program 
// passing "world"  as argument.
import java.io.InputStream;
import java.io.IOException;
public class CallHello {
    public static void main( String [] args ) throws IOException {
         Process child = new ProcessBuilder("java", "Hello", "world").start(); 
         // read byte by byte the output of that progam.
         InputStream in = child.getInputStream();
         int c = 0;
         while( ( c = in.read() ) != -1 ) {
            // and print it
            System.out.print( (char)c);
         }
     }
}

Output:

Hello world
又爬满兰若 2024-09-19 12:02:03

Child 是在 try...catch 块内声明的,因此其作用域是该块的本地范围。您正试图在块外访问它。你应该在块之前声明它,比如

Process child;
try {
// code
child = Runtime.getRuntime().exec(command);
// code
}
catch(/*blah blah*/) {}
// more code

Child is declared inside the try...catch block so its scope is local to that block. You're trying to access it outside of the block. You should declare it before the block, something like

Process child;
try {
// code
child = Runtime.getRuntime().exec(command);
// code
}
catch(/*blah blah*/) {}
// more code
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文