Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch a Scala Swing app on Windows without command window

I have a Swing app written in Scala in a .jar file. I made a desktop shortcut with the command scala "filepath\filename.jar" which works, except it brings up a command window first which sits in the background until I close the application. I want to make this go away.

I believe for Java you're supposed to use javaw instead of java for this purpose. I tried copying the scala.bat file to a new file called scalaw.bat to and changing line 24 to

if exist "%JAVA_HOME%\bin\javaw.exe" set "_JAVACMD=%JAVA_HOME%\bin\javaw.exe" 

and line 28 to

if "%_JAVACMD%"=="" set _JAVACMD=javaw

but (after updating the desktop shortcut) I'm still getting the command window coming up. Any ideas?


Update: I tried adding scala-library.jar to the manifest but it doesn't work, I guess because paths are relative to the jar's root. Other option seems to be include the whole scala-library jar within the jar file (Creating a jar file from a Scala file), but I don't consider that a solution.

Or I can unpack the jar to its constituent .class files and use the shortcut

javaw.exe -cp "%SCALA_HOME%/lib/scala-library.jar";"filepath\\" packageName.className

but ... that involves unpacking the jar.

Another semi-workaround Ive found is to edit the shortcut so that it the command window runs minimized.


Solution: as per eugener's answer, you need to use javaw without the -jar option, but specifying the jar-file e.g.

javaw -cp "%SCALA_HOME%/lib/*";"C:/path/yourjar.jar" packageName.className

If it's a Swing app and you use the scala-swing library you'll need to include that as well, or use the * wildcard as above.

like image 357
Luigi Plinge Avatar asked Sep 10 '11 14:09

Luigi Plinge


2 Answers

I would advise to launch your app directly using javaw. Since Scala compiles into the same byte code as Java - you can do it. Don't forget to add standard scala libraries to the class path

like image 117
Eugene Ryzhikov Avatar answered Oct 19 '22 23:10

Eugene Ryzhikov


It's clearly stated in the java command explanation, about -jar:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

Making a shortcut with javaw.exe -cp "%SCALA_HOME%\lib\scala-library.jar";"%SCALA_HOME%\lib\scala-swing.jar";"C:/path/yourjar.jar" packageName.mainClassName did work.

The solution is hence include libraries + packaged target code with -cp argument and don't use the -jar switch. As also given as a solution in the question.

like image 28
Epicurist Avatar answered Oct 20 '22 00:10

Epicurist