1

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();
    }
}
user3898085
  • 47
  • 1
  • 6

4 Answers4

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.

0

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.

Community
  • 1
  • 1
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
0

The way windows handles double-clicking a jar file is by launching javaw which is designed to run a Java GUI program without displaying the console window. When I double-click a Java console application, I see something like this in the console window:

javaw -jar "C:\JavaPrograms\SampleConsoleApp\dist\DistanceCalc.jar"

Nothing else is displayed because javaw is designed to launch a GUI window, but there is none.

If you would like to make a console application that gets launched with a double click, create a text file called launcher.bat and place the launch command in that batch file:

java -jar "C:\JavaPrograms\SampleConsoleApp\dist\DistanceCalc.jar"

Netbeans will actually generate this command line for you when you run your program from the IDE by displaying something like this in the output window:

To run this application from the command line without Ant, try:
java -jar "F:\JavaPrograms\IndexMaker\dist\IndexMaker.jar"
Thorn
  • 4,015
  • 4
  • 23
  • 42
  • This is what I ended up doing: making a bat file and running it with it. Still, kinda annoying not being able to just run my program (I wanted to give it to a friend, so now I have to fix the bat file), but it worked nontheless. Thank you a lot. – user3898085 Aug 09 '14 at 03:45
0
  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)

lewis4u
  • 14,256
  • 18
  • 107
  • 148