Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Iam a beginer programmer java on Debian Linux 12 (Later may be on FreeBSD 14 too) and I would like to make an small application a Java Swing launcher to launch some java web servers like Jetty, Glassfish and Wildfly for webdevelopment and to run some HTML 5 templates (to test them how they run in each Java Web Server).I found two Java source codes (one to make the Java Swing interface and one which run the unix shell script) the only thing is that I don't know how to integrate or how to make java to run the unix command inside of a Java Swing app. Is it possible to that ? and also is there any way to create a new button to stop the process of a Java Server Launcher? , here is the code of the Java Server Launcher :

Java
import java.awt.*;
import javax.swing.*;

public class JavaServerLauncher {

    static JFrame frame;
    static JPanel panel, panelData;
    static JMenuBar menuBar;
    static JMenu menuFile, menuSetting, menuOption;
    static JMenuItem itemNew, itemOpen, itemSave, itemPrint, itemOption, itemClose, itemView, itemReports;
    static JToolBar toolBar;
    static Icon iconMenu = UIManager.getIcon("html.pendingImage");
    static JButton barSave, barEdit, barClear, barDelete;
    static ButtonGroup group;
    static JRadioButtonMenuItem subFont1, subFont2, subFont3, subFont4, subFont5;
    static JCheckBoxMenuItem checkPrefer;
    static JLabel label;
    static JTextField textFirst, textMiddle, textLast;
    static JCheckBox checkGender;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                gui();
            }
        });
    }

    public static void gui() {

        frame = new JFrame("Java Server Launcher for Debian Linux");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(550, 300);

        JavaServerLauncher myMenu = new JavaServerLauncher();
        myMenu.myMenuBar();
        myMenu.myToolBar();
        frame.setJMenuBar(menuBar);
        frame.add(toolBar, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
    }

    public void myToolBar() {

        toolBar = new JToolBar(JToolBar.HORIZONTAL);
        frame.add(toolBar);


        barSave = new JButton("Apache TomCat", iconMenu);
        toolBar.add(barSave);

        barEdit = new JButton("Jetty Server", iconMenu);
        toolBar.add(barEdit);

        barClear = new JButton("GlassFish 7", iconMenu);
        toolBar.add(barClear);

        barDelete = new JButton("Wild Fly", iconMenu);
        toolBar.add(barDelete);
        
        barDelete = new JButton("Turn Off", iconMenu);
        toolBar.add(barDelete);
    }

    public void myMenuBar() {
        menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

    }
}


What I have tried:

I have checked google it in some web pages but not answer, some users told me Process Builder but I cannot find any source code with Process Builder with Java Swing.
Posted
Updated 16-Jan-24 9:03am
v2

1 solution

As already suggested to you you need to use ProcessBuilder (Java SE 18 & JDK 18)[^]. The documentation shows how to build a command and use this and associated classes to start it. And here is a tutorial with sample code: https://www.baeldung.com/java-lang-processbuilder-api[^].
 
Share this answer
 
Comments
ManuelGNUBSD 16-Jan-24 15:30pm    
Thanks for the link I will check the information about! but I do have one question how can I use ProcessBuilder inside a Java Swing button ? do you know any link with a example ? that would be really useful !
Richard MacCutchan 16-Jan-24 16:44pm    
You call it from inside your button press handler.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900