Click to See Complete Forum and Search --> : running files in java
agent_x91
10-04-2004, 04:07 PM
I know how to open streams to read or write to files, but is it possible to make a java application which can run or open another file?
ie. when program 1 is executed, it also executes program 2.
NogDog
10-04-2004, 04:30 PM
http://javaalmanac.com/egs/java.lang/Exec.html
HaganeNoKokoro
10-04-2004, 04:34 PM
There's a twist to this (at least on Windows machines). It works the way they say there for real executables, but for dos-type commands like "dir" you would have to make the command your java executes "cmd /c dir"
agent_x91
10-04-2004, 05:17 PM
thanks, that's really helpful! are those commands like command-line commands?
ray326
10-04-2004, 11:53 PM
Indeed they are all "command lines" for commands but the ones HaganeNoKokoro refers to (in Windows) are actually functions embedded in the command interpreter, cmd.exe or command.com.
agent_x91
10-05-2004, 01:08 PM
I'm confused about this command thing. I tried what they said on the website NogDog posted, but it always finds an IOException.
What am I doing wrong? can someone give me an example of a working command?
this is the code i copied from the website:
try {
// Execute a command without arguments
String command = "ls";
Process child = Runtime.getRuntime().exec(command);
// Execute a command with an argument
command = "ls /tmp";
child = Runtime.getRuntime().exec(command);
} catch (IOException e) {
}
I assumed I would need to import java.io.*; as well.
It compiles correctly, but never works.
help!:( :confused: :confused: :confused:
ray326
10-05-2004, 02:53 PM
What OS are you running this on? Those particular commands are for *nix systems, DOS/Win. Also IIRC you need to specify the full path to the command *unless* exec() spawns a shell and passes your command to it. The shell is what finds stuff on the PATH. Try printing the IOException to see but it may be "file not found."
agent_x91
10-05-2004, 03:00 PM
I have suse linux, but I haven't used it yet.
My OS is Windows XP Professional.
ray326
10-05-2004, 03:05 PM
XP don't know "ls" from shine-ola (and I don't know XP from shine-ola) so try "dir" or "cmd dir" or the old DOS style "cmd /c dir" and see what happens. And print out that execption instead of just eating it.
agent_x91
10-05-2004, 03:08 PM
I've already tried another command and sometimes I've still gotten the error, sometimes it hasn't thrown an Exception, but still has done nothing. But I'll try printing it and see what I find now.
agent_x91
10-05-2004, 03:12 PM
okay, the IOException when I triedt he command "dir":
java.io.IOException: CreateProcess: dir error=2
when I tried "cmd dir", nothing was printed at all - i guess it didn't throw a IOException, but it didn't do anything else either.
and when I tried "cmd /c dir", it was also blank...
HaganeNoKokoro
10-05-2004, 03:54 PM
This will print out the dir command output:
import java.io.*;
class RunProc {
public static void main(String args[]) {
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("cmd /c dir");
InputStream inStd = pr.getInputStream();
InputStreamReader inStdR = new InputStreamReader(inStd);
BufferedReader bStd = new BufferedReader(inStdR);
String line=null;
while((line=bStd.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}
agent_x91
10-05-2004, 03:56 PM
ah, the information must be grabbed by a stream? maybe that's where i went wrong... okay I'll try your example and modify it. thanks for the help.
By the way, I've seen it before and I'm sure it does something simple, but what does the "Exception.printStackTrace()" function print exactly?
agent_x91
10-05-2004, 03:59 PM
thanks a lot, the application works this time. I'll fiddle about with it and check how it works lol. thanks again.
HaganeNoKokoro
10-05-2004, 04:04 PM
It gives more detail about what happened when the exception was thrown
example: when trying to rt.exec("dir");
toString() generates
java.io.IOException: CreateProcess: dir error=2
printStackTrace() generates
java.io.IOException: CreateProcess: dir error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Unknown Source)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at RunProc.main(RunProc.java:7)
agent_x91
10-05-2004, 04:05 PM
ah right thanks I'll remember that
ray326
10-05-2004, 11:23 PM
If you're not using Eclipse for your Java development then I'd recommend you give it a try. It not only will help with compile errors, it will also show you your options on the fly. E.g. in your exception handler when you type 'e.' it will give you a list of all of e's public attributes and methods including toString(), printStackTrace(), getMessage() and all the others.