在没有命令窗口的 Windows 上启动 Scala Swing 应用程序

发布于 2024-12-04 10:21:57 字数 1341 浏览 1 评论 0原文

我有一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角卖回忆 2024-12-11 10:21:57

我建议直接使用 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

时光匆匆的小流年 2024-12-11 10:21:57

java 中明确说明了这一点命令解释,关于-jar

当您使用此选项时,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:

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.

各自安好 2024-12-11 10:21:57

您可以尝试将 bat 文件编译为 exe(我使用快速批处理文件编译器)。我相信这会阻止命令窗口的出现。我已经很长时间没有使用 Windows,但我记得它有时对我有帮助。

You can try to compile your bat file to exe (I used quick 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文