• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Facing problem with running external exe from my java code

 
suchi patil
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have to run the exe from java code for which I m using Runtime and Process classes. The exe runs in the following way on command prompt

C:/ myexe -j [now press enter]
input sentence ! [now press enter]

And after this exe gives the output.

So is there any way to achieve this programmatically in Java?


here is my java code

import java.io.*;
class CallToExe
{

static BufferedReader br;

public static void main(String[] args)
{
File F = new File("C:/Chasen.exe");
try
{
if( F.exists())
{
System.out.println("Exe exists");
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("C:/chasen.exe");

InputStream in = proc.getInputStream();
br = new BufferedReader(new InputStreamReader(in));

int a = in.read();
System.out.println(a);

String res = br.readLine();
System.out.println(res);

while(!res.equals(null))
{
System.out.println(res);
res = br.readLine();
}
proc.waitFor();
proc.destroy();
}
}
catch(NullPointerException npe)
{
npe.printStackTrace();
}
catch (Exception IOEx)
{
IOEx.printStackTrace();
}
}
}

I am not able to pass the arguments to exe which consists of '[-j] [press enter key] [statemet !] [press enter key] [press enter key ]'

Thanks in advance
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a number of pitfalls to be aware of when using Runtime.exec; this article exlains how to avoid some of them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic