0

I am trying to run the following program

import java.io.*;
class classname
{  public static void main(String args[])throws IOException
{
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(reader);
int t=Integer.parseInt(in.readLine());
System.out.println(t);
} }

I have created a seperate file input_file in which i have the input as 89

But when i try to execute the following code through

Process p=runtime.getRuntime().exec("java classname <input_file" );

It does not executes

I am trying to know that how can execute these file redirection commands using java Runtime or ProcessBuilder.

I am trying to create a web interface The web interface is something like online compiler e.g. ideone.com

The user will have its input in the input textarea and code in the code are

When user will click on compile it will show the output

But i cant the redirect the output of my input file for java classname

Rajat Bhadauria
  • 260
  • 2
  • 12
  • It does not execute...... => does it show an exception?? Is there anything you can tell what does happen? – Blaatz0r Feb 16 '15 at 09:45
  • I ran the following command through jsp Process p=runtime.getRuntime().exec(“java classname – Rajat Bhadauria Feb 16 '15 at 09:48
  • With jsp do you mean JavaServer pages?? – Blaatz0r Feb 16 '15 at 09:51
  • yes java server pages – Rajat Bhadauria Feb 16 '15 at 09:55
  • If the process variable is null then most probably an exception is thrown since exec throws one of these 4: SecurityException - If a security manager exists and its checkExec method doesn't allow creation of the subprocess IOException - If an I/O error occurs NullPointerException - If command is null IllegalArgumentException - If command is empty if it is not NULL then try to retrieve the ExitValue from the process and see what it returns. – Blaatz0r Feb 16 '15 at 09:59

1 Answers1

0

The java command doesn't implement redirection. For that you need to run an enclosing command shell, for example sh -c or cmd /c.

user207421
  • 305,947
  • 44
  • 307
  • 483