executing a .class from a java file with java commands in it


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 <img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/tuSNM.jpg" data-original="https://i.stack.imgur.com/tuSNM.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

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


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 <a href="https://stackoverflow.com/questions/11336157/running-external-program-with-redirected-stdin-and-stdout-from-java" rel="nofollow">Running external program with redirected stdin and stdout from Java</a>

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


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


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


  • How to read data from socket connection - android
  • C# where to add a method
  • C++ String tokenisation from 3D .obj files
  • Determining the length of a read stream in node js
  • vi mode to emacs mode while on R
  • How to handle empty space in url when downloading image from web?
  • incomplete type 'struct' error in C
  • Problem with rejecting incoming call
  • How to copy styled text in JTextPane
  • Reading a file into a multidimensional array
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Using a canvas object in a thread to do simple animations - Java
  • wxPython: displaying multiple widgets in same frame
  • Converting a WriteableBitmap image ToArray in UWP
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Why value captured by reference in lambda is broken? [duplicate]
  • Using $this when not in object context
  • req.body is undefined - nodejs
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Volley JsonObjectRequest send headers in GET Request
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • javascript inside java/jsp code
  • Perl system calls when running as another user using sudo
  • sending/ receiving email in Java
  • Javascript convert timezone issue
  • How to delete a row from a dynamic generate table using jquery?
  • Android Studio and gradle
  • Proper way to use connect-multiparty with express.js?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Is there any way to bind data to data.frame by some index?
  • Checking variable from a different class in C#
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?