Runnable Jar and multiple java versions

I sent a running can to a friend. He clicks on it twice and it doesn't open it crashing. However, my friend can open it.

I believe the problem is that my first friend has multiple java versions on his machine and he is using the wrong version when double clicked.

How can I use the correct java version to run my jar? I'm fine for my friend to do this from the command line.

+2


a source to share


2 answers


He could just take the correct java version as part of his command line execute statement. If, for example, you are compiling with Java6, it might do something like the following in windows:

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

      

It just runs this command wherever the jar file is. This assumes, of course, that it has JRE 1.6.0_13 installed - change it to whatever version it runs. In addition, the path will be different for different systems.

The other options on Windows are that you can specify different Java versions to be used by default via the java configuration in the Control Panel, if I remember correctly.




Edit:

Alternatively, you can change the executable java

in the command to javaw

if you want it to run in the background - that is, if it's a Swing application in the first place and no console is required.

+1


a source


I process it by creating 1 file in my project with -target 1.3

. This 1 file checks the JVM version and displays a message if the user needs to upgrade the JRE. The main class in the manifest points to this class. This gives the user something more enjoyable than an accident.



    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);
                    }
                });
            }
        }
    }

      

+3


a source







All Articles