1

I am new to creating batch files. I followed a tutorial and wrote a program to redirect the output of an echo programs's simple Java version into the file test.txt. I am supposed to get output as:

E:\classes\com\javaworld\jpitfalls\article2>java GoodWinRedirect test.txt (Tutorials's example) OUTPUT>'Hello World' ExitValue: 0

Instead, when i type C:\Users\attsuap1\Desktop\JavaCallingBatchFile2\src>GoodwinRedirect.java test.txt in my command prompt, a WordPad page opens, showing the codes that is in the java class.

If i type C:\Users\attsuap1\Desktop\JavaCallingBatchFile2\src>java GoodWinRedirect test.txt, I get the error:

Error: Could not find or load main class GoodWinRedirect

These are the codes:

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();
    }
  }
}

How do i get the output as OUTPUT>'Hello World' ExitValue: 0 in the command prompt? Someone please help me. Thank you so much in advance.

  • well looks like the class is simply not there... did you compile it first with `javac`? – Eugene Nov 15 '17 at 08:34
  • What is the class that i should create? I did not compile. –  Nov 15 '17 at 08:44
  • 1
    you first invoke `javac GoodwinRedirect.java` - this will create a `GoodwinRedirect.class`, then you invoke it via `java GoodwinRedirect` – Eugene Nov 15 '17 at 08:45
  • 2
    Possible duplicate of [How do I run a Java program from the command line on Windows?](https://stackoverflow.com/questions/16137713/how-do-i-run-a-java-program-from-the-command-line-on-windows) – Bernhard Barker Nov 15 '17 at 08:48
  • The codes in my question are the codes from the GoodWinRedirect.java class that i created –  Nov 15 '17 at 08:52

0 Answers0