Java 在命令提示符而不是写字板中获取输出
我不熟悉创建批处理文件。我按照教程编写了一个程序,将echo程序的简单Java版本的输出重定向到test.txt文件中。我应该得到以下输出:Java 在命令提示符而不是写字板中获取输出,java,batch-file,Java,Batch File,我不熟悉创建批处理文件。我按照教程编写了一个程序,将echo程序的简单Java版本的输出重定向到test.txt文件中。我应该得到以下输出: E:\classes\com\javaworld\jpitfalls\article2>java GoodWinRedirect test.txt(教程示例) 输出>“你好,世界” ExitValue:0 相反,当我打字时 C:\Users\attsuap1\Desktop\JavaCallingBatchFile2\src>GoodwinRedirect
E:\classes\com\javaworld\jpitfalls\article2>java GoodWinRedirect test.txt(教程示例)
输出>“你好,世界”
ExitValue:0
相反,当我打字时
C:\Users\attsuap1\Desktop\JavaCallingBatchFile2\src>GoodwinRedirect.java test.txt
在我的命令提示符下,打开一个写字板页面,显示java类中的代码
如果我打字
C:\Users\attsuap1\Desktop\JavaCallingBatchFile2\src>java GoodWinRedirect test.txt
,
我得到一个错误:
错误:无法找到或加载主类GoodWinRedirect
以下是代码:
GoodWinRedirect.java
import java.util.*;
import java.io.*;
class StreamGobbler extends Thread {
InputStream is;
String type;
OutputStream os;
StreamGobbler(InputStream is, String type) {
this(is, type, null);
}
StreamGobbler(InputStream is, String type, OutputStream redirect) {
this.is = is;
this.type = type;
this.os = redirect;
}
public void run() {
try {
PrintWriter pw = null;
if (os != null)
pw = new PrintWriter(os);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (pw != null)
pw.println(line);
System.out.println(type + ">" + line);
}
if (pw != null)
pw.flush();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public class GoodWinRedirect {
public static void main(String args[]) {
if (args.length < 1) {
System.out.println("USAGE java GoodWinRedirect <outputfile>");
System.exit(1);
}
try {
FileOutputStream fos = new FileOutputStream(args[0]);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("java jecho 'Hello World'");
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT", fos);
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
fos.flush();
fos.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
import java.util.*;
导入java.io.*;
类StreamGobbler扩展线程{
输入流为;
字符串类型;
输出流;
StreamGobbler(InputStream是,字符串类型){
这(is,type,null);
}
StreamGobbler(InputStream是,字符串类型,OutputStream重定向){
this.is=is;
this.type=type;
this.os=重定向;
}
公开募捐{
试一试{
PrintWriter pw=null;
如果(os!=null)
pw=新的打印机(os);
InputStreamReader isr=新的InputStreamReader(is);
BufferedReader br=新的BufferedReader(isr);
字符串行=null;
而((line=br.readLine())!=null){
如果(pw!=null)
pw.println(行);
系统输出打印项次(类型+“>”+行);
}
如果(pw!=null)
pw.flush();
}
捕获(ioe异常ioe){
ioe.printStackTrace();
}
}
}
公共类GoodWinRedirect{
公共静态void main(字符串参数[]){
如果(参数长度<1){
System.out.println(“用法java GoodWinRedirect”);
系统出口(1);
}
试一试{
FileOutputStream fos=新的FileOutputStream(args[0]);
Runtime rt=Runtime.getRuntime();
processproc=rt.exec(“javajecho'helloworld'”);
//有错误信息吗?
StreamGobbler errorGobbler=新的StreamGobbler(proc.getErrorStream(),“ERROR”);
//有输出吗?
StreamGobbler outputGobbler=新的StreamGobbler(proc.getInputStream(),“OUTPUT”,fos);
//把他们踢出去
errorGobbler.start();
outputGobbler.start();
//有错误吗???
int exitVal=proc.waitFor();
System.out.println(“ExitValue:+exitVal”);
fos.flush();
fos.close();
}捕获(可丢弃的t){
t、 printStackTrace();
}
}
}
如何在命令提示符中获得输出为“Hello World”ExitValue:0?有人请帮帮我。提前非常感谢。看来课程根本不在那里。。。您是先用
javac
编译的吗?我应该创建什么类?我没有编译。您首先调用javac GoodwinRedirect.java
-这将创建一个GoodwinRedirect.class
,然后您通过java GoodwinRedirect
调用它。我的问题中的代码可能是我创建的GoodwinRedirect.java类中的代码。看起来该类根本不存在。。。您是先用javac
编译的吗?我应该创建什么类?我没有编译。您首先调用javac GoodwinRedirect.java
-这将创建一个GoodwinRedirect.class
,然后通过java GoodwinRedirect
调用它。我问题中的代码可能是我创建的GoodwinRedirect.java类中的代码