Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help launching external executable files from a Java program

Status
Not open for further replies.

brinker

Programmer
May 31, 2001
48
0
0
CA
Hello,

I am trying to launch and executable file from witin a Java program (under a Windows 98 and Unix platform). I am using the following code to launch the executable using Windows:

import java.io.IOException;

public class WorkingWithExeFiles
{
public static void main(String argv[])
{
Runtime rt = Runtime.getRuntime();
try
{
rt.exec("test2.exe");

} catch(IOException ioe){ ioe.printStackTrace();}
System.out.println("\nwhat is wrong");
System.exit(0);
}
}

This java file runs and does not produce any errors. However, it does not appear to launch the executable. The executable was developed using Visual C++ and it produces a dos window which asks the user to enter in various values. The exeutable runs fine from windows explorer, but it the code above refuses to work from within Java. Does anybody have any suggestions that might work for both a windows and Unix environment? (I understand that the executable to be lauched from within the Java program must be compliled in each envionment seperately).

thanks

Narjit
 
Hi.

Are you sure you put the right path to launch the exe?
You need to explicit the path of the exe in the string, the classpath won't change anything.

Bye.
--
Globos
 
It often helped with Java paths if I just put a slash (like "/test2.exe") before the filename and put the file into the classpath... allow thyself to be the spark that lights the fire
 
your code is incomplete for successfully executing an external application. here's a piece of sample code. the variable 'extCommand' is the external program name (of course, make sure the right path is applied).

this code does work for running external Perl program. i do believe it works for other type of applications since how the external program is written doesn't matter.

good luck.

Runtime rt = Runtime.getRuntime();
try {
Process extProcess = rt.exec(extCommand);
extProcess.waitFor();

// receive the output of external program
InputStream extInput = extProcess.getInputStream();
int bytesAvailable = extInput.available();
if (bytesAvailable > 0)
{
byte[] extInputData = new byte[bytesAvailable];
extInput.read(extInputData);
System.out.println(new String(extInputData));
}
}
catch (Exception e)
{
}
 
Hello everyone,

It appears that I can run an external application using my code above so long as the external code itself produces it's own window. However, if the executable code does not produce it's own window, then there is a problem seeing any output. For instance if the C file has a printf("\nsomething") statement, I cannot see this when using the code above.

If anybody know a solution to this problem, I would be very appreciative of hearing about it.

thanks
 
Hi Brinker,

The reason why "something" isn't written to the console is because another outputstream is created for the runtime process.

This doesn't mean that your C program isn't work. If you have noticed WhiteSox's example above, the method exec() actually returns an instance of the class Process and through the instance, you will be able to get an inputStream. If you loop through the inputstream and print out every single line, you will be able to see "something" printed out.

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
It appears that in WhiteSox's code above, the extProcess.waitFor line causes the program to enter into an infinite frozen state. I am pretty sure this is the line at which the freeze up occurs after going through the code line by line. I wonder if there is a way around this problem?

Also, I still see no output from the executable file, even if I remove the extProcess.waitFor() command. This is the case under windows and unix. The exeutable file produced from C++ asks the user to input two numbers, adds the numbers up, and generates the output. The C++ program is very simple so just to test how to exeucte the program under Java. The C++ program actually is :

#include <iostream.h>

int main(void)

{
int i;
cout << &quot;Enter the value of i:&quot;;
cin >> i;
cout << &quot;i is &quot;<<i<<endl;
return 0;
}


It is very simple, and I only wish to observe the output from the file after launching it in a Java program.

Thanks

Brinker
 
Hi Brinker,

Like I have said in the previous post, the console for the runtime process and the runtime for your java app is different. Take the 2 classes below for example. &quot;example1&quot; will execute &quot;example2&quot;. I have used WhiteSox's code and put it into &quot;example1&quot;. If you run &quot;example1&quot;, you will see the String &quot;testing&quot; printed out.

The difference between this and your program is your C program requires user input before it is able to print to the runtime's console. Since you can't see your C program's console, the program will always be waiting for the user for input hence the infinite loop.


// example1
import java.io.*;
public class example1
{
public static void main(String[] args)
{
Runtime rt = Runtime.getRuntime();
try {
Process extProcess = rt.exec(&quot;java example2&quot;);
extProcess.waitFor();

// receive the output of external program
InputStream extInput = extProcess.getInputStream();
int bytesAvailable = extInput.available();
if (bytesAvailable > 0)
{
byte[] extInputData = new byte[bytesAvailable];
extInput.read(extInputData);
System.out.println(new String(extInputData));
}
}
catch (Exception e)
{
}
}
}


// example2
public class example2
{
public static void main(String[] args)
{
System.out.println(&quot;testing&quot;);
}
}

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top