I currently have this code:

import java.util.Random;
import java.io.*;
public class randomdwarf {
public static void main(String[]args)throws IOException { 
String dwarves[]=
	{"Dopey", "Sneezy", "Happy", "Sleepy", "Grumpy", "Bashful",	"Doc"};
	Random generator = new Random();
	int randomIndex = generator.nextInt( 7 );
	System.out.println(dwarves[randomIndex]);
	Runtime.getRuntime().exec("H:\\Profile\\Desktop\\dwarves\\Doc.jpeg");
}
}

It is a very simple code that is designed to get a random dwarf name then display the image. Note that currently I am just testing the program out and have it only open the picture of "Doc". I will implement the random image opening once I figure out how to open the image. After searching for a while I have not been able to find a solution. I am currently using windows and would appreciate any help you can give me.

Thanks,


Jaro

Runtime.exec isn't going to do what you need.
Assuming you are on Java 1.6 or 1.7 (which you should be) you can use the new Desktop class to open any arbitrary file in whatever application the OS associates with that file type (eg Image Viewer for jpegs).
This tutorial tells you everything you could possibly want to know (and a lot more)...
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/

but the short version is:

Desktop d = Desktop.getDesktop();
File f = new File("H:\\Profile\\Desktop\\dwarves\\Doc.jpeg");
d.open(f);

this is how i got my images the last time i had to do that, theres plenty of ways to do it, i think i remember early classes teaching to use ImageIcon instead of Images and setting the icon of the component instead of drawing an image, but whatever way you choose is fine as long as it works right?

//selection du bon personage a dessiner et de son nombre de membre a dessiner
Image img = null;
try {
    img = ImageIO.read(this.getClass().getResource("Images\\pendu"+character[i]+"-"+nbErreurs[i]+".png"));
} catch (IOException e) {
    e.printStackTrace();
}

//dessiner pendu
gPanel.drawImage(img, 50, 50, null);

gPanel here is a Graphics object that i created with :

gPanel = jpn_pendu[i].getGraphics(); //jpn_pendu is a JPanel array

(not writing this post because zero's way isn't good, just giving OP some options :))

EDIT : Just realized that OP wants to OPEN and image not draw one, ill leave this code just incase he changes is mind but ignore my post for your current problem! sorry :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.