在没有命令窗口的 Windows 上启动 Scala Swing 应用程序
我有一个用 Scala 编写的 Swing 应用程序,位于 .jar
文件中。我使用命令 scala "filepath\filename.jar"
创建了一个桌面快捷方式,它可以工作,只是它首先会弹出一个命令窗口,该窗口位于后台,直到我关闭应用程序。我想让这件事消失。
我相信对于 Java,您应该使用 javaw
而不是 java
来达到此目的。我尝试将 scala.bat 文件复制到名为 scalaw.bat 的新文件中,并将第 24 行更改为,
if exist "%JAVA_HOME%\bin\javaw.exe" set "_JAVACMD=%JAVA_HOME%\bin\javaw.exe"
将第 28 行更改为,
if "%_JAVACMD%"=="" set _JAVACMD=javaw
但是(更新桌面快捷方式后)我'我仍然出现命令窗口。有什么想法吗?
更新:我尝试将 scala-library.jar
添加到清单中,但它不起作用,我猜是因为路径是相对于 jar 的根目录的。其他选项似乎是在 jar 文件中包含整个 scala-library jar (从 Scala 文件创建 jar 文件),但我不认为这是一个解决方案。
或者,我可以将 jar 解压到其组成的 .class
文件,并使用快捷方式
javaw.exe -cp "%SCALA_HOME%/lib/scala-library.jar";"filepath\\" packageName.className
但是...这涉及到解压 jar。
我发现的另一个半解决方法是编辑快捷方式,以便命令窗口最小化运行。
解决方案:根据 eugener 的回答,您需要使用 javaw
不带 -jar 选项,但指定 jar 文件,例如
javaw -cp "%SCALA_HOME%/lib/*";"C:/path/yourjar.jar" packageName.className
如果它是 Swing应用程序,并且您使用 scala-swing
库,您还需要包含该库,或者使用上面的 *
通配符。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议直接使用 javaw 启动您的应用程序。由于 Scala 编译成与 Java 相同的字节码 - 你可以做到。不要忘记将标准 scala 库添加到类路径中
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
java
中明确说明了这一点命令解释,关于-jar
:制作快捷方式
javaw.exe -cp "%SCALA_HOME%\lib\scala-library.jar";"%SCALA_HOME%\lib\scala-swing.jar";"C:/path/yourjar.jar" packageName.mainClassName
做了工作。
因此,解决方案是包含库+带有
-cp
参数的打包目标代码,并且不使用-jar
开关。也作为问题中的解决方案给出。It's clearly stated in the
java
command explanation, about-jar
: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.您可以尝试将
bat
文件编译为exe
(我使用快速批处理文件编译器
)。我相信这会阻止命令窗口的出现。我已经很长时间没有使用 Windows,但我记得它有时对我有帮助。You can try to compile your
bat
file toexe
(I usedquick batch file compiler
). I believe this prevent command window appearance. I did not use Windows for a long time, but I remember it helped me sometimes.