Linux下Java路径问题/Javac java -cp

在Linux环境中,当编译Java程序并遇到运行时找不到jar包的问题时,可以使用-classpath或-cp参数解决。如果test.java和所需jar包在同一目录/home/demo下,可以通过指定-classpath或-cp来引用多个jar包,如`javac -classpath ./test.jar test.java`。运行时同样需要正确设置路径,例如`java -classpath :./test.jar test`。如果遇到“找不到或无法加载主类”的错误,检查classpath路径末尾是否有冒号。要编译多个文件,可以使用`javac *.java`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Linux系统下,编译Java源程序时没有错,运行时出现找不到jar包问题;下面的-cp就是-classpath的缩写,功能一样。下面就来讨论下路径问题:

如果你的程序test.java和jar包test.jar都在/home/demo文件夹下:

1)怎么引用多个jar包

  这需要我们设置classpath 或者cp 来引入多个jar包,这个大家都知道,但是多个jar包之间一定要用:隔开而不是;或者,

编译时,eg:javac -classpath ./test.jar test.java或者javac -cp ./test.jar test.java

2)怎么引用编译好的基类

   同引用jar包原理是一样的也是通过classpath 或者cp中间也要用:隔开此处也是填写路径最后不要用/结束

运行时,eg:eg:javac -classpath :./test.jar test或者javac -cp :./test.jar test

3)都写好了,提示 “错误: 找不到或无法加载主类 org.apache....." 是什么原因

       去看下cp后边最后一个路径后面是否:结尾如果不是,最后要以:结尾

4)怎么编译多个文件

       *.java即可

$ ./gradlew cleanBuildCache Starting a Gradle Daemon (subsequent builds will be faster) FAILURE: Build failed with an exception. * What went wrong: Unable to start the daemon process. This problem might be caused by incorrect configuration of the daemon. For example, an unrecognized jvm option is used.For more details on the daemon, please refer to https://docs.gradle.org/8.10.2-milestone-1/userguide/gradle_daemon.html in the Gradle documentation. Process command line: /usr/lib/jvm/java-11-openjdk-amd64/bin/java --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED -Xmx2048m -Dfile.encoding=UTF-8 -Duser.country=CA -Duser.language=en -Duser.variant -cp /home/ts/.gradle/wrapper/dists/gradle-8.10.2-milestone-1-bin/9qvixjbuxjevivtpr9fhsty1d/gradle-8.10.2-milestone-1/lib/gradle-daemon-main-8.10.2.jar -javaagent:/home/ts/.gradle/wrapper/dists/gradle-8.10.2-milestone-1-bin/9qvixjbuxjevivtpr9fhsty1d/gradle-8.10.2-milestone-1/lib/agents/gradle-instrumentation-agent-8.10.2.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.10.2-milestone-1 Please read the following process output to find out more: ----------------------- FAILURE: Build failed with an exception. * What went wrong: org/objectweb/asm/Type * Try: > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. * Exception is: java.lang.NoClassDefFoundError: org/objectweb/asm/Type at org.gradle.initialization.DefaultLegacyTypesSupport.<clinit>(DefaultLegacyTypesSupport.java:37) at org.gradle.interna
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值