using runtime.exec to open pictures/video/sound
I am trying to use runtime exec to open 3 different file types. i am trying to use this code, i don't receive errors but nothing happens. any help will be apreciated. Thanks
try{
String cmd = ("open");
String file= ("C:\\pic1.jpg");
Runtime.getRuntime().exec(cmd + file);
catch(IOException e){};
put a stackTrace in your exception to receive the errors... and you will see what's going on
[url http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Toolkit.html]Toolkit[/url]Toolkit.getDefaultToolkit().getImage(/* File Path */);You could use that to display an Image in a JFrame or whatever you want to do.
Also, the correct syntax for a try...catch block is this:
try {
//do stuff
} catch(Exception e) {
//do stuff
}
Couple things here (and I'm not a MS windows user)
1) is open a real command? If you execute (in a command shell)
open c:\\pic1.jpg
does that do anything?
2) You are directly concatenating the strings with no space, so try entering this in a command shell:
openC:\\pic1.jpg
Most likely that won't work.
3) NEVER complain about not "receiving errors" if you do this:
catch (IOException ioe) {}
> Couple things here (and I'm not a MS windows user)> 1) is open a real command? If you execute (in a> command shell)> open c:\\pic1.jpg> does that do anything?No, you just need to type in the name of the file.
this won't work, try this instead
try {
String [] cmds = {"cmd","/c","C:\\pic1.jpg"};
Process p = Runtime.getRuntime().exec(cmds);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
~Tim
Message was edited by:
SomeoneElse
i don't know what i was thinking not putting in a stackTrace after my exception. sorry for that.
i tried the code that Tim recommended but nothing loaded. By doing this should it open the .jpg in the defualt picture viewer in windows?
i don't get an ioexception this time...is there other error catchers i should put in to see if i get something?
thanks for your help.
[url] http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[],%20java.io.File)[/url]You can read what exceptions will be thrown there.
I don't know what the problem you are having is then. When I run the code I posted, the jpg opens using the default windows picture viewer, even if I go into explorer and delete the file type. I guess window knows that file extension and how to open it . If I remove the /c part, then nothing happens, and no exceptions are thrown, so make sure you have that, and make sure it is a forward slash, not a back slash.
~Tim
here is my complete code.
private void comboBoxActionPerformed(java.awt.event.ActionEvent evt){
try{
System.out.println("got to prev images action");
String[] file = {"cmd","/c","C:\\pic1"};
Process p = Runtime.getRuntime().exec(file);
}catch(IOException e){
e.printStackTrace;
}catch(NullPointerExceptin npe){
npe.printStackTrace;
}catch(IllegalArgumentException iae){
iae.printStackTrace();
};
}//end action performed
there might be a typo in the code because i am doing the coding on my laptop which isn't connected to the internet.
is there something i should be doing with the process p?
String[] file = {"cmd","/c","C:\\pic1"};What type of file is pic1?
thanks for the help, i just got it to work. the file extension said .jpeg, but it never workedi tried .jpg and it opened fine. Thanks for all of your help i really appreciate it.
will this process work the same for sound and video
> will this process work the same for sound and videoYes.