I've made a command line application in Java using Eclipse. I've created a runnable JAR and understand that if I want to run it, I need to do it via command prompt. Is there any way to have the .jar open a command prompt and run the program on click?
Edit: To clarify, the program works in Eclipse's console, and running the .jar I created through the command prompt works (i.e. java -jar Minesweeper.jar). I'd just like it to execute like that when I click on the .jar file.
I've noticed this problem when trying to run a command line base app as a runnable jar file. It works fine if you try to do something like creating and showing a jframe. My only suggestion is if you are on windows you can make a batch file that has the line:
java -jar nameofjar.jar
Name the file something like run.bat and then you can double click on that and it will run the command line app. Other than that im not sure why this problem occurs with command line input based applications.
EDIT:
Here is a way to make a fake command prompt to display to if you cant figure out a way to get the real one to run from double clicking the jar.
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));
}
}
}
Also, can you tell me the steps you are using to create the runnable jar file. The code above should run by double clicking on the jar file. If it doesn't, then something might be wrong with the way you are creating the jar file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With