0
package package1;

public class Protection
{
 private int pri=1;
 protected int pro=2;
 public int pub=3;

 public void show()
 {
  System.out.println("Value of private variable is :"+pri);
  System.out.println("Value of protected variable is :"+pro);
  System.out.println("Value of public variable is :"+pub);
 } 
 public static void main(String s[])
 {
  Protection p=new Protection();
  p.show();
  //p.pro=0;
 }
} 

The above code shows a runtime error indicating :

Could not find or load class Protection

I have tried and created a package1 folder, but it still does not work at all.

tux3
  • 7,171
  • 6
  • 39
  • 51
Tiny Jaguar
  • 433
  • 3
  • 12
  • 30
  • Please show how you're compiling and running the code, including how the files are laid out on disk. – Jon Skeet Mar 09 '15 at 09:49
  • Also let us know if you're using IDE and set up build path. – Sachin Verma Mar 09 '15 at 09:50
  • I think its a path issue .How you are setting the env variable? – singhakash Mar 09 '15 at 09:51
  • @JonSkeet : The path where files reside is F:\java. I am working on Windows 8.1 machine with Java version 1.8.25. I am compiling via command prompt F:\java>javac Protection.java F:\java>java Protection Error: Could not find or load main class Protection – Tiny Jaguar Mar 09 '15 at 09:53
  • @SachinVerma : no IDE used, only notepad++ – Tiny Jaguar Mar 09 '15 at 09:53
  • You need to give the full qualified name. Refer to this answer - http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean – Yellen Mar 09 '15 at 09:54
  • @yellen : already done that F:\java\package1>javac Protection.java F:\java\package1>java package1.Protection Error: Could not find or load main class package1.Protection – Tiny Jaguar Mar 09 '15 at 09:58
  • Are you running it as java Protection or java Protection.class ? – Yellen Mar 09 '15 at 09:58
  • @yellen : I tried even Protection.class with and without package too. F:\java>javac Protection.java F:\java>java Protection.class Error: Could not find or load main class Protection.class F:\java>java package1.Protection.class Error: Could not find or load main class package1.Protection.class – Tiny Jaguar Mar 09 '15 at 10:01

4 Answers4

2

Try this:

F:\java\javac -d . Protection.java
F:\java\java package1.Protection

Why does compiling without -d flag fail? That is because the package declaration says class has a package but it is not in the right directory for the run time to execute. Therefore either you have to place the source file Protection.java in package1 directory and go from there as suggested by @Ashutosh or use -d flag so that the generated class file is placed in the right directory structure.

And .. this works with the assumption that your CLASSPATH is empty or has . (current directory) in CLASSPATH.

RaviH
  • 3,544
  • 2
  • 15
  • 14
  • 1
    Does that mean that though we compile in same directory as the package name, it will still show error? Because the below commands still showed error. F:\java\package1>javac Protection.java F:\java\package1>java Protection Error: Could not find or load main class Protection – Tiny Jaguar Mar 09 '15 at 10:23
  • 1
    @HarshMadhani Class file should be in the package directory structure from the directory where you run java. If you do what you mentioned in the above comment you should do cd .. to move to F:\java directory and run it from there. – RaviH Mar 09 '15 at 10:27
  • Does that mean that it does not matter where does the source files (.java) are present, all it matters is where the byte-code(.class) resides for proper execution??? – Tiny Jaguar Mar 09 '15 at 10:32
  • 1
    You can think like that for now. But that does not mean it is not possible to execute class files from other locations. You can do lot of magic by writing your own class loader and load the class from other locations too. However, that is advanced topic. Don't worry about it for now. I brought it up so that you will "remain hungry and remain foolish" (in Steve Jobs' words) and dig more after you learn the fundamentals of java. – RaviH Mar 09 '15 at 10:36
1

Here.. this might helpenter image description here

Follow the things in the screen cdapture

Yellen
  • 1,785
  • 16
  • 35
0

I have saved your java file in folder package1 as Protection.java. In the command prompt from the location where package1 folder is placed ran following command javac package1/Protection.java

then

java package1.Protection

It's working for me. Please try same steps or try to find what you are missing.

TimeTraveler
  • 1,223
  • 12
  • 15
0

check your class path(F:\java)..that would be two class files having same name. go through on What does "Could not find or load main class" mean?

Community
  • 1
  • 1
venkat
  • 503
  • 6
  • 15