IceWreck
IceWreck

Reputation: 127

Run Non GUI Jar Files

I know that I can run non GUI jar files from the command line. Is there any way that can do so by clicking or something and not writing the commands again and again.? Is there any software to do so. ( I am talking about a compiled jar and don't want to run from any ide)

Upvotes: 0

Views: 494

Answers (2)

barwnikk
barwnikk

Reputation: 976

public static final String TITLE = "CONSOLE title";
public static final String FILENAME = "myjar.jar";
public static void main(String[] args) throws InterruptedException, IOException {
    if(args.length==0 || !args[args.length-1].equals("terminal")) {
        String[] command;
        if(System.getProperty("os.name").toLowerCase().contains("win")) {
            command = new String[]{"cmd", "/c", "start \"title \\\""+TITLE+"\\\" & java -jar \\\""+new File(FILENAME).getAbsolutePath()+"\\\" terminal\""};
        } else {
            command =new String[]{"sh", "-c", "gnome-terminal -t \""+TITLE+"\" -x sh -c \"java -jar \\\""+new File(FILENAME).getAbsolutePath()+"\\\" terminal\""};
        }
        try {
            Process p  = Runtime.getRuntime().exec(command);
            p.waitFor();
        } catch(Throwable t){
            t.printStackTrace();
        }
    } else {
        //THERE IS YOUR CONSOLE PROGRAM:
        System.out.println("Hey! What's your name?");
        String read = new BufferedReader(new InputStreamReader(System.in)).readLine();
        System.out.println("Hey, "+read+"!");
        Thread.sleep(10000);
    }
}

You can run it with double clicking on .jar file. Don't forget about MANIFEST.MF! :) (working on linux, also!)

Example (I only double clicked on jar file): Example

Upvotes: 1

niqueco
niqueco

Reputation: 2408

The way intended by Java is that you call java -jar XXXX.jar on the jars you need. Drawback is that you can't specify a classpath so all classes should be there.

A cooler way to package an application is by using Java WebStart. With that the user installs the application jut by clicking on a web browser. Check here http://docs.oracle.com/javase/8/docs/technotes/guides/javaws/developersguide/contents.html

Upvotes: 0

Related Questions