0

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.

Community
  • 1
  • 1
Hellmy
  • 76
  • 6
  • maybe deliver a batch file? Disadvantage is that you have 2 files, but this should be no problem? – Danny. May 28 '14 at 10:45
  • Yeah would be possible but I really would prefer just a single file, because it should be a single download. Perhaps I could start a terminal from java and then run there the spring context!? – Hellmy May 28 '14 at 10:48
  • I found [this answer](http://stackoverflow.com/a/18467548/2468948) for starting the application within a new terminal window. But with that code it only works on windows (I think), so I have to distinguish all the possible systems. But a nearly solution – Hellmy May 28 '14 at 11:18

1 Answers1

0

You could write a simple .bat file. Just write the cmd statements in a file and save it as "random".bat

Mansouritta
  • 166
  • 1
  • 1
  • 10