Two Laptop Bag*
The moose likes I/O and Streams and the fly likes   Facing problem with running external exe from my java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "  Facing problem with running external exe from my java code " Watch "  Facing problem with running external exe from my java code " New topic
Author

Facing problem with running external exe from my java code

suchi patil
Greenhorn

Joined: Feb 12, 2006
Posts: 3
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
Marshal

Joined: Mar 22, 2005
Posts: 41065
    
  43
There are a number of pitfalls to be aware of when using Runtime.exec; this article exlains how to avoid some of them.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Facing problem with running external exe from my java code
 
Similar Threads
Send a String to Server and server send it somewhere on network
no such element? eek!
Reading a file from a directory.
Java Runtime command syntax help
how can i use shell32 in my java program