请在下面的java代码中澄清这个问题
我修改了这个方法。。。但出现了以下错误:请在下面的java代码中澄清这个问题,java,runtime-error,Java,Runtime Error,我修改了这个方法。。。但出现了以下错误: 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
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();
}
}
您确实忽略了返回的和流
这将是一个很长的故事,所以这里只有一个链接:。阅读全部四页。该代码没有问题 它所做的是在内部执行另一个Java程序 类
进程
有一个获取程序输出的方法,如果你想看到结果,你必须将该输出重定向到你自己的
下面是一个使用Runtime.exec的“现代”替代品的示例
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
输出:
// 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);
}
}
}
Child在try…catch块内声明,因此它的作用域是该块的本地范围。你试图在街区外访问它。你应该在块之前声明它,比如
Hello world
如果在每行代码前添加四个空格,StackOverflow将很好地格式化。
child
不在范围内,请扩展该catch
,将您的代码与我的答案中的代码进行比较。谢谢:)无论如何,我无法找到解决方案..:(那很快。你读了所有的4页了吗?到目前为止你有什么?现在发生了什么?
Process child;
try {
// code
child = Runtime.getRuntime().exec(command);
// code
}
catch(/*blah blah*/) {}
// more code