Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Facing problem with running external exe from my java code

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic