0
import java.io.*;

public class chk 
{
String command;
public String  getMsg(String fileName,File Path1) 
{
    String dir,name=" ";
    int x;
    x=fileName.indexOf(".class");name=fileName.substring(0, x);
    command ="java " + name +" < C:\\iptest\\input.txt > C:\\outtest\\"+name+".txt";
    String output = executeCommand(command,Path1);
    if(output.compareTo("")==0)             
        output = "Compilation Successfull!!";
    return output;
}
private String executeCommand(String command,File Path1) 
{
    StringBuffer output = new StringBuffer();
    Process p;
    try 
    {
        p = Runtime.getRuntime().exec(command,null,Path1);
        //p.waitFor();
        BufferedReader reader1 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        BufferedReader reader2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";           
        while ((line = reader1.readLine())!= null) 
        {
            output.append(line + "\n");
        }
        while ((line = reader2.readLine())!= null) 
        {
            output.append(line + "\n");
        }
    } catch (Exception e) 
    {
        e.printStackTrace();
    }
    return output.toString();
}
public static void main(String args[])throws IOException
{
    String x;
    File dir=new File("C:\\Users\\RONEET\\Desktop");
    chk ob=new chk();
    x=ob.getMsg("hello.class",dir);
    System.out.println("OUtput : "+x);
}
 }

What I am doing in this file is i am executing a hello.class file from a java file and storing its output as a txt file at the following location C:\outtest\ with a proper file name. But when i compile the above file my program goes into some kind of infinite loop and never terminates .

window stays like this enter image description here

EDITED : hello.java

import java.io.*;
class hello
{
public static void main(String agrs[])throws IOException
{
    BufferedReader s=new BufferedReader(new InputStreamReader(System.in));
    String str;
    str=s.readLine();
    System.out.print(str+"\n");
}
}
6
  • 2
    Can we see what your Hello class does? Commented Feb 14, 2014 at 17:23
  • Why is one Java program invoking another? Would it not make more sense to use the Hello class directly? Commented Feb 14, 2014 at 17:24
  • @JohnGaughan different application have different requirements mine needs this that is why i am working on it Commented Feb 14, 2014 at 17:28
  • @rick that is the reason for the first word in my comment: "why." I was curious about the requirement. Maybe there is a better way? Commented Feb 14, 2014 at 17:31
  • @JohnGaughan I am building an application that can compile programs written on different languages . Commented Feb 14, 2014 at 17:34

2 Answers 2

1

disclaimer - I've figured out that my solution could work except if I used STDIN and STDOUT redirection with < and >. So I am using the same solution presented here Running external program with redirected stdin and stdout from Java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Runner {

    public static void pipeStream(InputStream input, OutputStream output) throws IOException {
        byte buffer[] = new byte[1024];
        int numRead = 0;

        do {
            numRead = input.read(buffer);
            output.write(buffer, 0, numRead);
        } while (input.available() > 0);

        output.flush();
    }

    public static void main(String[] argv) {
        File dir = new File("C:\\Users\\Leo\\workspace\\STackOverflow\\src\\");
        FileInputStream fileIn = null;
        FileOutputStream fileOut = null;

        OutputStream procIn = null;
        InputStream procOut = null;

        try {
            fileIn = new FileInputStream(new File(dir, "input.txt"));
            fileOut = new FileOutputStream(new File(dir, "output.txt"));

            Process process = Runtime.getRuntime().exec("C:\\jdk1.7.0_51\\bin\\java Hello", null, dir);
            procIn = process.getOutputStream();
            procOut = process.getInputStream();

            pipeStream(fileIn, procIn);
            pipeStream(procOut, fileOut);
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }
}

given

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Hello {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try{
            BufferedReader br = 
                          new BufferedReader(new InputStreamReader(System.in));

            String input;

            while((input=br.readLine())!=null){
                System.out.println("processed"+input);
            }

        }catch(IOException io){
            io.printStackTrace();
        }   

    }

}

this is pretty much the same as

"java Hello < input.txt > output.txt"

given input.txt like

1
2
3
4

it generates output.txt like

processed1
processed2
processed3
processed4
Sign up to request clarification or add additional context in comments.

7 Comments

I know, the two loggers are strange. You don't need them. But all the rest can help I think ;-)
how to execute "java hello < C:\iptest\input.txt > C:\outtest\hello.txt" command with this code of yours ?
It will be very helpful if you can edit your code with this command of mine
I am sorry, I am in a rush here :-(
if so, then help me :-) I am drowning in debts
|
0

The reading of the streams is probably blocking. You could try putting the stream reading into separate threads.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.