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