Xander
Xander

Reputation: 9171

Runnable Jar and multiple versions of java

I sent a runnable jar to a friend. He double clicks on it and it doens't open it crashes. However my other friend can open it.

I believe the problem is that my first friend has multiple versions of java on their machine and it is using the wrong version on the double click.

How can I have the correct version of java be used to run my jar? I am ok to have my friend do this from the command line.

Upvotes: 2

Views: 2000

Answers (2)

Devon_C_Miller
Devon_C_Miller

Reputation: 16528

I handle it by building 1 file in my project with -target 1.3. That 1 file checks the JVM version and displays a message if the user needs to update their JRE. The Main-class in the manifest points to this class. This gives the user something a bit nicer than a crash.

    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.SwingUtilities;
    public class JVMCheck {
        private JVMCheck() {}
        public static boolean isJVMOk()
        {
            boolean result = false;
            String s = System.getProperty("java.version", "undef");
            if (!s.equals("undef")) {
                String parts[] = s.split("[^0-9]+");
                if (parts.length >= 2) {
                    if (parts[0].equals("1") && parts[1].compareTo("6") >= 0) {
                        result = true;
                    }
                }
            }
            return result;
        }
        public static void main(String[] args)
        {
            if (JVMCheck.isJVMOk())
            {
                // Call you real main class here:
                // RealMainClass.main(args);
            }
            else
            {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run()
                    {
                        JOptionPane.showMessageDialog(null, 
                            new JLabel("<html><b>Error<b>: Unsupported JVM: \""
                                + System.getProperty("java.vm.name", "Undef") + "\" "
                                + System.getProperty("java.vm.version", "Undef")
                                + "<br>The JVM version required to run is 1.6.0<br>"
                                + "The 1.6 JRE may be downloaded from:<br>"
                                + "http://java.sun.com/javase"),
                                "Error", JOptionPane.ERROR_MESSAGE);
                        System.exit(0);
                    }
                });
            }
        }
    }

Upvotes: 3

aperkins
aperkins

Reputation: 13114

He could simply grab the correct version of the java as part of his execution statement on the command line. If, for instance, you are compiling with Java6, he could do something like the following in windows:

C:\Program Files\Java\jre1.6.0_13\bin\java -jar <myJarFileName>

He would simply run this command wherever the jar file is located. This is assuming, of course, that he has JRE 1.6.0_13 installed - change that out for whatever version he is running. Also, the path will be different for different systems.

Another options in Windows is that you can specify different versions of Java to be the default through the java configuration in the control panel, if I remember correctly.


Edit:

Additionally, you can change the java executable in the command to javaw if you want it to run in the background - i.e. if it is a Swing application primarily, and a console is not required.

Upvotes: 1

Related Questions