'Running a .jar file without a GUI

I made this little program for a project I'm working on, and it works as it should when I run from Eclipse. What I would like to do is have a .jar file that I can double click and have it run in my console. I alredy exported as a JAR and a runnable JAR on the Export menu, and both times it launches the Java process in the Task Manager, it uses both CPU and Memory, but nothing shows in my screen. I tried compiling with the command prompt, and it's just the same thing. So, my question is, can I have a stand alone java application without a GUI? Because it seems that's my problem: I followed a tutorial to make a program with one, and it worked just fine.

I'm using Windows 8 64-bit (I have to because of some other software I have to use on my project). I'm a newbie at this Java buissness, so if you can please keep it as simple as possible; I'd really appreciate it. Thanks in advance.

import java.util.Scanner;
public class Distancia {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int check = 1;
    System.out.println("Kilómetros a Millas Náuticas\n");
    do {
        int cm = 0;
        int salida = 0;
        while (salida != 1){
        try {
            System.out.println("Escriba la distancia en kilómetros:");
            cm = sc.nextInt();
            salida = 1;
        } catch (Exception e) {
            System.out.println("Opción Inválida");
            sc = new Scanner(System.in);
            sc.reset();
        }
        } //Fin del While
    //Conversiones de distancias
    int km = cm*50;     // Conversión de Centimetros-mapa a Kilómetros
    double mn = km/(1.8);   // Conversión a Millas Náuticas
    //Cálculos de Velocidades
    System.out.println("Distancia en kilómetros: "+km+" km.");
    System.out.println("Distancia en millas náuticas: "+mn+" MN.\n");
    if (mn/5<24)
        System.out.println("Tiempo a 05 nudos: "+mn/5+" horas.");
    else{
        System.out.println("Tiempo a 05 nudos: "+mn/5+" horas.");
        System.out.println("Tiempo a 05 nudos: "+(mn/5)/24+" días.");
        System.out.println();
    }
    if (mn/8<24)
        System.out.println("Tiempo a 08 nudos: "+mn/8+" horas.");
    else{
        System.out.println("Tiempo a 08 nudos: "+mn/8+" horas.");
        System.out.println("Tiempo a 08 nudos: "+(mn/8)/24+" días.");
        System.out.println();
    }
    if (mn/12<24){
        System.out.println("Tiempo a 12 nudos: "+mn/12+" horas.");
        System.out.println();
    }
    else{
        System.out.println("Tiempo a 12 nudos: "+mn/12+" horas.");
        System.out.println("Tiempo a 12 nudos: "+(mn/12)/24+" días.");
        System.out.println();
    }
    }
    while (check !=0);
    sc.close();
    }
}


Solution 1:[1]

I agree with what @lewis4u mentioned.Although his answer needs a little bit clarification for beginners. Since the first java application beginners try to implement in most cases will be a No-GUI Application. To create a java no-Gui console program that runs with a double click (follow the steps below):

  1. Write your java code and make sure to save the file with (.java) extension.
  2. Go to cmd and write the following commands:

(cd theDirectoryWhereTheJavaFileIsFound),then ENTER

(javac filename.java),then ENTER

This process is used to compile the java file to a class file.

  1. Create a new text file at the same folder, and write in it:

(java filename) then ENTER

(pause)

Then save the file, and change the extension of it from (.txt) to (.bat).

After doing the above steps, you are done!

When you double click the (.bat) file you created, this will launch a console application where you can input/output.

Solution 2:[2]

Of course you can.
All you need to do is having it run by the Java virtual machine.
That's

java -jar "\path\to\YOUR_JAR_NAME.jar" 

However, running it by double-click doesn't work.
But if you use C# instead of Java, it generates a lovely .exe that you can run by double-clicking.

Or if you insist on using Java, you can use Launch4j:
http://launch4j.sourceforge.net/

If the problem is, that it expects a GUI, you can execute the AttachConsole WinAPI call, to show the command line window.

Solution 3:[3]

  1. Open any text editor
  2. write this two lines:

    java "yourmainclassname"
    pause
    
  3. save that file as "name".bat

  4. Run it with double click from windows GUI

(of course this new created .bat file must be in the same folder as the .class)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abbas Borji
Solution 2 Community
Solution 3 lewis4u