admin管理员组文章数量:1399499
I understood how the classpath is necessary for running the project but I still have some doubt.
I compiled my class using
javac /Users/username/IdeaProjects/leetcodePrograms/src/problems/HouseRobberII213.java
after compiling, I did
java -cp /Users/username/IdeaProjects/leetcodePrograms/src/ problems.HouseRobberII213
this runs my program
but If I do
java -cp /Users/username/IdeaProjects/leetcodePrograms/ src.problems.HouseRobberII213
This gives
Error: Could not find or load main class src.problems.HouseRobberII213
Caused by: java.lang.NoClassDefFoundError: src/problems/HouseRobberII213 (wrong name: problems/HouseRobberII213)
Someone pls explain why this issue. How was /Users/username/IdeaProjects/leetcodePrograms/src/ decided as valid classpath but not /Users/username/IdeaProjects/leetcodePrograms
I understood how the classpath is necessary for running the project but I still have some doubt.
I compiled my class using
javac /Users/username/IdeaProjects/leetcodePrograms/src/problems/HouseRobberII213.java
after compiling, I did
java -cp /Users/username/IdeaProjects/leetcodePrograms/src/ problems.HouseRobberII213
this runs my program
but If I do
java -cp /Users/username/IdeaProjects/leetcodePrograms/ src.problems.HouseRobberII213
This gives
Error: Could not find or load main class src.problems.HouseRobberII213
Caused by: java.lang.NoClassDefFoundError: src/problems/HouseRobberII213 (wrong name: problems/HouseRobberII213)
Someone pls explain why this issue. How was /Users/username/IdeaProjects/leetcodePrograms/src/ decided as valid classpath but not /Users/username/IdeaProjects/leetcodePrograms
Share Improve this question asked Mar 25 at 3:40 Salil HardeniyaSalil Hardeniya 35 bronze badges 4 |2 Answers
Reset to default 1The class path is not invalid. It's just that there is no class called src.problems.HouseRobberII213
at the class path.
In HouseRobberII213.java, you have probably written the package declaration as:
package problems;
This means the fully qualified name of the class is problems.HouseRobberII213
, not src.problems.HouseRobberII213
.
If you had written
package src.problems;
Then there would indeed be a class called src.problems.HouseRobberII213
.
It depends on your package design and code.
In case your code is below and save it to /some/where
directory.
public class HouseRobber{
}
You need to comple it as below
cd /some/where
javac HouseRobber
Then you can add /some/where
to your environment parameter CLASSPATH
. So that you can launch your java program from any where.
java HouseRobber
In case your code is like this:
package something;
public class HouseRobber{
}
You need to add the directory above something
to CLASSPATH
. Because Java will search something.HouseRobber
from the directories in CLASSPATH
.
本文标签: how is Classpath decided in java what makes a valid classpathStack Overflow
版权声明:本文标题:how is Classpath decided in java? what makes a valid classpath? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744218417a2595746.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
src.problems.HouseRobberII213
. In the file that declaresHouseRobberII213
, you probably have writtenpackage problems;
, haven't you? If you have writtenpackage src.problems
, thensrc.problems.HouseRobberII213
would exist. – Sweeper Commented Mar 25 at 3:45