Running java files without ugly command prompt box in the background
Asked Answered
R

2

6

I made a java program with a GUI using eclipse so whenever I want to run it I run it there, but my friend wants to run it on his computer so I made a class-file using javac in command prompt and also made a batch-file to run the program. The problem is that every time ha runs it this command window is still in the background. So what I want is to make that box disappear, is it any way to do that?

Appreciate all answers :)

Recept answered 12/5, 2013 at 16:57 Comment(0)
D
14

Launch it with javaw instead of java. The Description for java mentions:

The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.

Dincolo answered 12/5, 2013 at 16:59 Comment(0)
D
0

Having another class to launch, I think is the best solution.

import java.io.IOException;
public class Main2 {

public static void main(String[] args) {

    try {
        String[] cmdArray = new String[ args.length + 1 ];
        cmdArray[0] = "javaw";
        for ( int a0=0; a0 < args.length; a0++ ) {
            cmdArray[ a0 +1 ] = args[a0];
        }
        Runtime.getRuntime().exec(  cmdArray ); 
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

}

Then you need a bat, with something like

@echo off
set TMP_CLASSPATH=%CLASSPATH%
cd c:\WorkDir
set CLASSPATH=C:\WorkDir\mayapps.jar
set CLASSPATH=%CLASSPATH%;C:\WorkDir\lib\others.jar

java -cp "%CLASSPATH%"  Main2 -splash:mysplash.gif -Dlog4j.configuration=myapp.log4j  mymainclass  -param1=xxx -param2 etc
Dahlgren answered 2/7, 2019 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.