-2

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

asked Mar 25, 2025 at 3:40
4
  • 1
    It's not about the class path. There is just no class called src.problems.HouseRobberII213. In the file that declares HouseRobberII213, you probably have written package problems;, haven't you? If you have written package src.problems, then src.problems.HouseRobberII213 would exist. Commented Mar 25, 2025 at 3:45
  • Yes, my class is in package problems; Ok, I understand now. 1 final question. If classpath declares the location of .class file and java will search inside given path, why java -cp /Users/username/IdeaProjects/leetcodePrograms/. problems.HouseRobberII213 does not work Commented Mar 25, 2025 at 4:01
  • The search is not recursive. It won't look for the class in subdirectories. Commented Mar 25, 2025 at 4:03
  • ok, thanks, now understood. Commented Mar 25, 2025 at 4:04

2 Answers 2

1

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

answered Mar 25, 2025 at 4:06
Sign up to request clarification or add additional context in comments.

Comments

0

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.

ahuemmer
2,14717 gold badges30 silver badges38 bronze badges
answered Mar 26, 2025 at 6:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.