由于外部库,Java可以在终端中编译

发布于 2025-02-02 16:54:19 字数 267 浏览 3 评论 0原文

我在Ubuntu 22.04尝试在终端运行我的代码。 该程序在VSCODE中也没有问题,也可以运行多个实例。 该程序由六个类文件组成。

当我尝试使用终端运行它时,就会发生麻烦。编译Java文件时 使用Javac它在我使用外部库的地方显示错误。

如果我使用VSCODE编译并以终端运行类文件,则会获得以下错误java.lang.classnotfoundexception

这会导致我问题,因为我也应该对该程序进行扩展。

I'm on Ubuntu 22.04 trying to run my code in the terminal.
The program works without problems in VScode, also when running multiple instances.
The program is consisted of six class files.

The trouble occurs when I try and run it with terminal. When compiling the java file
with javac it shows errors at places where I use the external libraries.

If I compile it with VScode and run the class file in terminal, I get the following error java.lang.ClassNotFoundException

This is causing me problems since I'm also supposed to dockerize the program.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

猥︴琐丶欲为 2025-02-09 16:54:19

您可以在street.json文件“ java.project.outputpath”中添加以下代码:“ bin”
这将是运行J​​ava代码时的bin文件夹中VS代码生成的.class文件。

您可以使用cd命令输入文件目录后使用Java命令。

You can add the following code in your setting.json file "java.project.outputPath": "bin",
This will be the .class file generated by VS Code in the bin folder of the same directory when running the Java code.

You can use the java command after entering the file directory with the cd command.

埋情葬爱 2025-02-09 16:54:19

这通常表明您要编译程序的类路径不包括库的正确路径。假设您的库是JAR文件,您的javac命令应该看起来像这样:

javac -cp libs/lib1.jar:libs/lib2.jar srcs/*.java

其中libs/是您库的相对路径,srcs/是您自己的Java文件的相对路径。

当您运行程序时,请确保您的类路径既包含库的位置又包括类文件的位置(在这种情况下是当前目录):

java -cp .:libs/lib1.jar:libs/lib2.jar <MainClass>

This generally indicates that the class path with which you're compiling your program does not include the correct paths to your libraries. Assuming your libraries are jar files, your javac command should look something like this:

javac -cp libs/lib1.jar:libs/lib2.jar srcs/*.java

where libs/ is the relative path to your libraries and srcs/ is the relative path to your own java files.

And when you run the program, make sure your class path includes both the locations of your libraries and the location of your class files (which in this case would be the current directory):

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