在命令提示符而不是写字板

时间:2017-11-15 08:21:23

标签: java batch-file

我是创建批处理文件的新手。我按照教程编写了一个程序,将echo程序的简单Java版本的输出重定向到test.txt文件中。我应该得到输出:

E:\classes\com\javaworld\jpitfalls\article2>java GoodWinRedirect test.txt (Tutorials's example) OUTPUT>'Hello World' 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();
    }
  }
}

如何在命令提示符下将输出作为OUTPUT>'Hello World' ExitValue: 0?有人请帮帮我。非常感谢你。

0 个答案:

没有答案