Creating an executable JAR file for the command line of a Java application

I made a command line application in Java using Eclipse. I created a runnable JAR and understand that if I want to run it, I need to do this using the command line. Is there a way to get .jar to open a command prompt and run the program when pressed?

Change . To clarify, the program runs in the Eclipse console and runs .jar created using the command line (i.e. java -jar Minesweeper.jar). I just would like this to be executed when I click on the .jar file.

+3
source share
2 answers

jar. , - jframe. - , :

java -jar nameofjar.jar

- run.bat, , . , , .

EDIT:

, , , .

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class FakeCommandPrompt {

private JTextArea myTextArea;
private JScrollPane scrollPane;
private JFrame mainFrame;

/**
 * Setup the fake command prompt.
 */
public FakeCommandPrompt()
{
    mainFrame = new JFrame();
    mainFrame.setBounds(new Rectangle(new Dimension(500, 400)));
    mainFrame.setBackground(Color.BLACK);

    myTextArea = new JTextArea();
    myTextArea.setBackground(Color.BLACK);
    myTextArea.setForeground(Color.WHITE);
    myTextArea.setEditable(false);
    myTextArea.setMargin(new Insets(10, 10, 10, 10));

    scrollPane = new JScrollPane(myTextArea);
    scrollPane.setBackground(Color.BLACK);

    mainFrame.add(scrollPane);
    mainFrame.setVisible(true);
}

public void printToCommandPrompt(String text)
{
    // Append the new next to the command prompt
    // Add a new line at the end
    this.myTextArea.append(text + "\n");
}

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    // Create and instance of the fake command prompt.
    FakeCommandPrompt commandPrompt = new FakeCommandPrompt();

    // Prints 0 -> 5 on the fake command prompt.
    for (int i = 0; i <= 5; i++)
    {
        commandPrompt.printToCommandPrompt(String.valueOf(i));
    }
}

}

, , jar. jar. , - , jar.

+2

Windows?

jar? ul > > > [ jar]

runnable jar, .

0

All Articles