Java代码在VSCode控制台中运行正常,但打包为.jar文件后在运行时出现问题。

huangapple go评论70阅读模式
英文:

Java code in vscode console not running correctly when packaged as .jar

问题

以下是您要的翻译部分:

我正在尝试编写一个简单的 Java 代码,当运行时,它将启动 Google 网站。以下是我的代码。

import java.awt.Desktop;
import java.net.URI;

class DoTheFunny {
    public static void main(String args[]) throws Exception {
        Desktop d = Desktop.getDesktop();
        d.browse(new URI("https://www.google.com"));
    }
}

此代码在 VSCode 中运行时是正常的。然而,当打包为 .jar 文件并运行时,没有任何反应。我尝试过转换为 .exe,同样没有任何反应。而且,当解压 .jar 文件时,我得到了正确的打包信息。我使用了 JAR Builder 扩展来打包我的代码。有人知道出了什么问题吗?

英文:

I am trying to write a simple code in java, that when run will launch the google website. Below is my code.

import java.awt.Desktop;
import java.net.URI;
class DoTheFunny{
    public static void main(String args[]) throws Exception{
        Desktop d=Desktop.getDesktop();
        d.browse(new URI("https://www.google.com"));
        }
    }

This code is working when run in vscode. However, when packaged as a .jar file and run, nothing happens. I have tried converting to a .exe and once again nothing happened. Also when extracting the .jar file I get the correct packaging information. I used the JAR Builder extension to package my code. Anyone have any idea what is wrong?

答案1

得分: 0

基本上,我会说我认为那并不是异常的。

即使我使用命令提示符和PowerShell,除非我

使用launch4j将其打包
或者在命令行上运行它。

要使用命令行运行您的JAR文件,可以使用java -jar的方式,如果它抱怨某个特定的主入口缺失,那么我认为您应该使用cfe选项重新制作您的JAR文件。

希望对您有帮助。

英文:

Basically, I would say that it I don't think that is abnormal.

Even when I use command prompt and powershell, it does the same thing unless I

Package it using launch4j
or run it on the command line.

To run your jar file using command line, use the java -jar way, if it complains about a certain main entry missing, then I think you should remake your jar file using the cfe option.

I hope I helped.

答案2

得分: 0

我认为 vscode 处理得有些宽容,如果你在命令行中使用以下方式运行你的 JAR 文件:java -jar DoTheFunny.jar,很可能会看到与无法访问类的成员有关的异常错误...

你应该将你的类声明为 public,这样当在 JVM 中运行时,Java 才会“知道”去哪里找到它。

在将你的类设为 public 之后,使用插件导出 JAR 文件,然后再次尝试,你可能会看到更好的结果。

希望这对你有帮助。

英文:

I think vscode is being a little forgiving, if you run your jar from the command line using: java -jar DoTheFunny.jar you'll likely see some type of exception error related to being unable to access a member of class...

You should declare your class as public, so that java "knows" where to find it when running it in the JVM.

After setting your class a public, export the jar using the plugin and try again, you'll probably see better results.

Let me know how that works out for you.

答案3

得分: 0

  1. 编译 .java 文件,生成 .class 文件:

    javac DoTheFunny.java

  2. 创建 .jar 文件:

    jar cvfe DoTheFunny.jar DoTheFunny .\

  3. 通过命令运行 .jar 文件,或在文件夹中双击运行:

    java -jar DoTheFunny.jar

英文:

1.Compile the .java file, which generates the .class file:

javac DoTheFunny.java

2.Create .jar:

jar cvfe DoTheFunny.jar DoTheFunny .\

3.Run the .jar by command or double-click it in the folder:

java -jar DoTheFunny.jar

Java代码在VSCode控制台中运行正常,但打包为.jar文件后在运行时出现问题。

huangapple
  • 本文由 发表于 2020年10月20日 05:20:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64435237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定