Hello guys

I'm new to writing java but have some experience with other languages. I use Eclipse as my IDE but when i try to run a simple console program (exported to .jar using Eclipse) i am only able to run it from the command prompt.

An example of a program that only seems to work in the cmd is:

package main;
import java.util.Scanner;

public class start {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        String text;
        text = input.nextLine();
        System.out.println(text);
    }

}

When i doubleclick it in the folder the computer seems to do something but appears finished within a fraction of a second.
The program works when i write the following in cmd:

cd Documents
java -jar test.jar

(It lets you write a line and prints the same line again.)

Can anyone tell me how i make the java program open a console window (cmd or whatever) to let the user enter the data and recieve the answer. And aditionally make it not disappear immediately after finishing the program.

Thanks alot in advance
Emil J.J.

Try reading an extra line before exiting the program to keep the console window open until the user enters something.

Also its useful in a program that is waiting for input from a user to print a message telling the user what is going to happen. In your program you want to have the user enter a line which you will read and display. Next you want to print a message telling the user to enter anything to exit the program. Then read whatever is entered and exit.

Thanks for the answer NormR1, and the solution with another input is a good idea :) But the main problem of this thread is that i can't get the .jar to work when i double-click it. Only when i run it from the command prompt, do i need to use any special commands for it to open its own window?

The double click is an OS thing. What command line is associated with a jar file?
I assume this is on Windows.
The javaw command does not use a command prompt window. Try adding a commandline that uses the java command and see it that works for you. The added command can be accessed by rightclicking on the jar file and selecting the command you have entered.
Here's how to look at the file type association table on WinXP:
Open Windows Explorer
Click on Tools menu
Click on Folder Options
Select File Types tab
Scroll down in the "Registered file types:" list until you see an entry for JAR.
Select the JAR entry.
Click the Advanced button
In the Edit File Type window scroll down the list of Actions until you see the one in bold.
Select the bold one
Click the Edit button
The command line is in the "Application used to perform action:" window.

Be careful not to change anything and Cancel your way out of the open windows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.