Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime class

 
Ajit Gupta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a problem while executing a c++ executable file frm my java code......if i use any other exe file other than one made by c++ ....it runs fine...but with c++ one it gives some environment error.....
so how do i run a c++ executable file frm my java code.....
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch!
There's no specific problem with running C++ programs from your Java apps. If you let us know what OS you're using, and what the specific error message(s) are, maybe we could help you figure out what's wrong.
 
Ajit Gupta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx a lot for replying....
actually i am using win 2k....
and there is no error message comin up..
its just that the exe file is not gettin executed....
coz it is a c++ executable file...i think it is not able to run in the java runtime environment(JRE)...which the exec method takes as default....]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So exec() isn't throwing an exception? That's how the error would be reported. If you're not getting an error message, why do you say that the program isn't being started?
If you're expecting the output to go to the console, it won't; the process's standard output goes to an InputStream that your Java program can read from (call Process.getInputStream() to get ahold of it.) If you want the output to go to the console, you'll have to read it, and then print it from Java.
 
Ajit Gupta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am expecting the output to b written in a text file...which will b created....and as the file is not being created i assume my exe file is not getting executed.....
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you better post the code where you set up the exec and handle the Process. Please use the CODE button to make the code more readable.
Bill
 
Ajit Gupta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("E:/gate_name.exe");
gate_name.exe is the file i want to execute.....
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, well, if the exec() doesn't throw an exception, then the program at least starts. One possibility is that the program prints a lot of text to standard output, and you're not reading it. One thing that confuses people sometimes is that if you start a process from Java, that process's output is held in a finte-sized buffer, and when that buffer fills up, the process is basically frozen until you make more room. So if your program is printing a lot of output and you're ignoring it, the program may be sleeping waiting for you to empty the buffer with p.getInputStream.read(). Perhaps it goes to sleep before the point where it creates this file.
 
Ajit Gupta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually the exe file i want to execute is an executable file of a c++ program...and this program shud when run create a text file and write some values into it....now when i run it myself independently the file is being generated...but not when i am trying to run it thru my java code....
so the exe file is not being executed....
coz i guess it cannot run in JRE....
the exec() command has a parameter for environment.....here as i am not specifying nething it takes it to b null and so by default JRE....and my exe file(c++ one) will not run in that...
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
coz i guess it cannot run in JRE....
the exec() command has a parameter for environment.....here as i am not specifying nething it takes it to b null and so by default JRE....and my exe file(c++ one) will not run in that...

We seem to have a major misconception here. When you exec a program, it does NOT run in the JRE environment - it gets a whole new operating system process to itself, WITH THE IMPORTANT POINT - which we keep saying but you don't seem to be reading - that if the program normally writes anything to std out, you have to provide for reading - your code is not doing that at present.
When you execute this program from a command line prompt, what happens? In addition to writing the file, that is.
What does the program require on the command line?
What variables in the environment?
Does it have to be executed with a specific "current directory"?
Bill
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and I would guess that some parts of windows are written in c++ too - event if they behave as written in VB
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ajit Gupta:

the exec() command has a parameter for environment.....here as i am not specifying nething it takes it to b null

There's one other possibile explanation hiding in this: if you're using one of the versions of exec() that takes an environment pointer, but actually passing "null", then the C++ program will be run with an empty envinronment. If you use one of the exec()s which don't accept this parameter, then the executed program will inherit the JVMs environment strings. So perhaps you're passing "null" as the environment, and therefore the C++ program can't find some needed DLL, or something else it needs (it used to be, a few years back, that on Windows 95, an exec()ed program wouldn't be able to find the Winsock libraries unless there was some specific item in the environment, for example.)

Garbage in, garbage out.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think programs you start this way may have trouble if they're doing stdout or errout and you're not hooking up to those streams. If you run the C++ program from the command line, does it display or require keyboard input? You might try connecting readers to the stdout and errout on the process you get from exec(). Last time I did this, I put them in their own threads so they can pull from either or both in any order.
 
Ajit Gupta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
william answering ur questions....
the exe file when run from the command prompt does not require any input from command prompt coz it takes its input from another text file....
and it is not meant to give any other output except writing to the text file...and yes it is being executed when we run from its own directory..
from any other place it gives " the file cannot be opened"
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from any other place it gives " the file cannot be opened"

AHA - it appears likely that your program depends on the "current" directory to locate the file. If you had been reading std out and std err like we suggested you would probably be seeing that error message - as it is, the program may be trying to write it and just hanging.
If you can't supply a complete path to the file you may have to write a batch file to change to the correct directory and then execute the program.
Bill
 
Ajit Gupta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx a lot....
everything said and done, the problem solved...
but one more chaos
how do i create a jar file from my class file
whenever i try it gives main class not found....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic