A spring boot application can start with a double-click on the .jar, but opens no terminal or something else, so a tomcat is running on port 8080 and I have to kill it from the task manager (what's a bit annoying when running different java applications). Is there a way to force spring/java to open a terminal or create a run context with a new window?
I know I could open the jar from a terminal with java -jar application.jar
and when killing the terminal also the server is killed. But for customer needs it would be interesting to have a double-click solution.
Thanks
Edit: Right now my main class looks
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
Edit #2 - Possible Solution:
Through the comment I got the idea about opening a terminal window. What is possible but the command differs for every system. So the testing with different systems is a bit complicated.
So my actual solution is to open a java window and redirect the console output to this window. Which I found here and just edited a few lines, so the java window looks now like this:
import java.awt.BorderLayout;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class TextAreaOutputStreamTest extends JPanel {
private JTextArea textArea = new JTextArea(15, 30);
private TextAreaOutputStream taOutputStream = new TextAreaOutputStream(
textArea, "> ");
public TextAreaOutputStreamTest() {
setLayout(new BorderLayout());
add(new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
System.setOut(new PrintStream(taOutputStream));
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Output");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TextAreaOutputStreamTest());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void mainRunner(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class TextAreaOutputStream extends OutputStream {
private final JTextArea textArea;
private final StringBuilder sb = new StringBuilder();
private String title;
public TextAreaOutputStream(final JTextArea textArea, String title) {
this.textArea = textArea;
this.title = title;
sb.append(title);
}
@Override
public void flush() {
}
@Override
public void close() {
}
@Override
public void write(int b) throws IOException {
if (b == '\r')
return;
if (b == '\n') {
final String text = sb.toString() + "\n";
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(text);
}
});
sb.setLength(0);
sb.append(title);
return;
}
sb.append((char) b);
}
}
and I call it from my main method with
public static void main(String[] args) throws IOException {
if (args.length == 0) {
TextAreaOutputStreamTest.mainRunner(args);
}
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
With this I control the behavior also through command line arguments.
Different solutions are welcome but right now this works for my purpose.