打包为 .jar 时,vscode 控制台中的 Java 代码无法正确运行 [英] Java code in vscode console not running correctly when packaged as .jar

查看:73
本文介绍了打包为 .jar 时,vscode 控制台中的 Java 代码无法正确运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 Java 编写一个简单的代码,运行时将启动 google 网站.下面是我的代码.

import java.awt.Desktop;导入 java.net.URI;类 DoTheFunny{public static void main(String args[]) 抛出异常{桌面 d=Desktop.getDesktop();d.browse(new URI(https://www.google.com"));}}

此代码在 vscode 中运行时有效.但是,当打包为 .jar 文件并运行时,没有任何反应.我曾尝试转换为 .exe,但再次没有任何反应.此外,在提取 .jar 文件时,我得到了正确的包装信息.我使用了

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.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

这篇关于打包为 .jar 时,vscode 控制台中的 Java 代码无法正确运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆