javalinuxexecutable-jar

Issues creating runnable jar files


I've been spending quite a bit of time trying to create executable jar files on linux. I can run jar files that I've downloaded by just double clicking on them, however when I try to do the same with jar files I create either with intellij or through the command line, I can't just open it by double clicking. I have to open a terminal and then run "java -jar jarfile.jar". It's not anything to do with default apps (I'm pretty sure) because both the jar files I've downloaded (e.x. ATLauncher) and the jar files I make have both been set to open with java 21.0.1.


Solution

  • Here is a quick test.

    import javax.swing.*;
    
    public class ShowVersion{
    
        public static void main(String[] args){
            JFrame frame = new JFrame("hello world");
            frame.add( new JLabel("java version: " + System.getProperty("java.version")));
            frame.pack();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
    }
    

    That will create a window and show the java version. Compile it.

    javac --target 8 --source 8 ShowVersion.java 
    

    That will make a version that works with java as old as java 8. Then create an executable jar file.

    jar cfe app.jar ShowVersion ShowVersion.class
    

    I expect the resulting jar file should run, and confirm if you're using the java you think you are.