Archived

This topic is now archived and is closed to further replies.

Macke

[java] Execute a program from another program

Recommended Posts

Macke    122
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Macke    122
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
>> 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

Share this post


Link to post
Share on other sites
bslayerw    122
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

Share this post


Link to post
Share on other sites