Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disconnect java application from console/command window [duplicate]

Possible Duplicate:
Is there a way to the hide win32 launch console from a Java program (if possible without JNI)

When I run my java swing application in a batch file on windows, the console/command window remains open whilst my java application is running. This creates an extra window on my taskbar which I would prefer not to have. But when I close the command window it stops my java application. Is there a way, perhaps via the batch file or command line parameters or code changes to my application, to have java.exe exit after bringing up my swing app and the console window close whilst my application still runs?

Main method is as follows:

public static void main(String args[]) {

    ApplContext applContext = new ApplContext();
    Throwable error = null;

    try {
        applContext.setUserConfigDir(args.length>0 ? args[0] : null);
        applContext.loadData();
        ApplTools.initLookAndFeel(Parameters.P_NIMBUS_LAF.of(applContext.getParameters()));
    } catch (Throwable e) {
        error = e;
    }

    // JWorkSheet is a JFrame.
    new JWorkSheet(applContext, error).setVisible();
}    
like image 276
tekumara Avatar asked Feb 28 '23 06:02

tekumara


1 Answers

Run your application with javaw.exe rather than java. If you're running from a bat file, use this in combination with the start command:

> start javaw.exe -jar myapp.jar

When run in this mode, it's a good idea to set up proper logging or at least redirect your output streams if you rely on the console for any debugging. For example, with no console, you'll never see those friendly stack traces printed for unhandled exceptions.

Note: java.exe is a Windows console application. As such, no matter how it is started, or what threads are running in it, a console will be allocated for it. This is the reason that javaw.exe exists.

like image 179
Dave Ray Avatar answered Apr 28 '23 22:04

Dave Ray