[java] Execute a program from another program

Started by
5 comments, last by Macke 22 years, 5 months ago
Hello! I wanted to execute a file from another program. Remember that there are functions in C++ and Visual Basic for doing this. Then there must be in java. Does anyone know of such function?
Advertisement
Java is different than VB and C++... it''s an internet based interpreter, while VB is an OS/machine based interpreter, and C++ is an os/machine compiler. Java can''t access programs located on the users machine (at least I didn''t think it could!). It may be able to run/launch other java applications based on the server (or where it''s running from), but I''m not that familiar with it.

Billy
I dont know. But Im programming java that have no internet relation. Im not making any java-script or things like that. A program that you will run on your computer. I found functions for writing and reading files.

Anyone knows how to execute files? If Im not completely wrong I recall that it was called something like shell or something in c. But im not certain.
Check out java.lang.Runtime.exec() in the java docs.

e.g. Runtime.getRuntime().exec("my_program.exe");
>> Java is different than VB and C++... it''s an internet based interpreter >>

LOL, sounds like someone has been learning Java from the New York Times

Henry
To the anonymous poster (good idea to post anon if you have no idea). Please don't make assumptions, if you are not 100% sure then don't post. It's misleading.... here is a code snippet to create a process and read feedback from the process (any text the program pumps to the output stream). The method should return onces the external program has completed. You could probably start this method in a new thread if you wanted to return immediately.

//--------------------------------------------------------------
public static void executeCommand(String cmd)
{
try
{
Process process = Runtime.getRuntime().exec(cmd);
InputStream processInputStream = process.getInputStream();
BufferedInputStream bufIn = new BufferedInputStream(processInputStream);

StringBuffer s = new StringBuffer();
char c = (char)bufIn.read();
while((byte)c != -1)
{
s.append(c);
c = (char)bufIn.read();
}
System.out.println(s);
bufIn.close();
process = null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
//--------------------------------------------------------------

Edited by - bslayerw on November 7, 2001 6:32:00 PM
I wanted to say Thanks!

You guys are the greatest!

This topic is closed to new replies.

Advertisement