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.
CodePudding user response:
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>
CodePudding user response:
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.