By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,032 Members | 825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,032 IT Pros & Developers. It's quick & easy.

Calling foreign executables

P: n/a
How can i call some other program from java application?

let say i want to call iexplorer('iexplore' from command line)...
thnx
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hrvoje Somun wrote:
How can i call some other program from java application?

let say i want to call iexplorer('iexplore' from command line)...
thnx


Maybe this could help you as an example, but I would suggest to do some more
eductional reading on java ;-)

import Java.lang.*;
import Java.io.*;

public class RuntimeExecTest {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
String[] callAndArgs = { "Notepad.exe",
"D:\\TEMP\\Test.txt" };
try {
Process child = rt.exec(callAndArgs);
child.waitFor();
System.out.println("Process exit code is:
" + child.exitValue());
}
catch(IOException e) {
System.err.println(
"IOException starting process!");
}
catch(InterruptedException e) {
System.err.println(
"Interrupted waiting for process!");
}
}
}

--
Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb
Anti Spam = remove the "dot" and the "at"
Registered Linux User #264690
Jul 17 '05 #2

P: n/a

"Hrvoje Somun" <hr****************@zg.tel.hr> wrote in message
news:br**********@sunce.iskon.hr...

How can i call some other program from java application?

let say i want to call iexplorer('iexplore' from command line)...
thnx


You may care to try the code below.

I hope this helps.

Anthony Borla

// ---------------------------------------------

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class LaunchIE
{
public static void main(String[] args)
{
if (args.length < 1)
{
System.err.println("Usage: java LaunchIE filename");
System.exit(1);
}

try
{
viewFile(new File(args[0]));
}

catch (FileNotFoundException e)
{
System.err.println("File could not be located, or does not exist");
}

catch (IOException e)
{
System.err.println("File could not be opened");
}
}

// Win32-specific Code
public static void viewFile(File file)
throws FileNotFoundException, IOException
{
if (!file.isFile())
throw new FileNotFoundException();

StringBuffer cmd =
new StringBuffer("rundll32 SHELL32.DLL,ShellExec_RunDLL ");

cmd.append(file.getAbsolutePath());

Runtime.getRuntime().exec(cmd.toString());
}
}

// --------------------------------------------

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.