0

Here's my Situation: I have a folder Structure:

C:\Users\user\Desktop\JavaTraining\Chapter3\examples.

examples is also a folder. Now, I have a file name Calculator.Java in Chapter3 folder with the package statement package Chapter3;. So, from the Command Line I compiled the file from the JavaTraining directory as javac Chapter3\Calculator.java, It compiled and I see a file Calculator.class generated. But when I run the command java Chapter3.Calculator from the JavaTraining Directory. It throwed me an error: Could not find file or load main class Chapter3.Calculator.

Then, I created a sub folder in Chapter3 named examples and copied the Calculator.java into the examples folder and tried compiling and executing the file thinking Chapter3 as the root folder ( executed commands from the Chapter3 directory). No error, the file got executed.

Please can anyone explain me why this happened or what is the reason behind it, I am going mad...

Calculator.java is just a class Calculator with main function trying to find a sum from a printsum function of two variables.

I went through the suggestions provided in http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean According to the above, it was either the syntax mistake ( the way of trying to executing the file) or setting the PATH and CLASSPATH environment variables.

I even tried the echo %CLASSPATH% to see if my CLASSPATH variable is set to current directory. It did show me the . when I executed the echo command from JavaTraining directory. The file did not execute when I tried Chapter3 folder as root directory, but when I create a subfolder in Chapter3 and made Chapter3 as the root directory, it executed, what might be the reason or what am I doing wrong,

Here is the command line with the output:

C:\Users\vikas\Desktop\JavaTraining>javac Chapter3\Calculator.java

C:\Users\vikas\Desktop\JavaTraining>java Chapter3.Calculator
Error: Could not find or load main class Chapter3.Calculator

C:\Users\vikas\Desktop\JavaTraining>cd Chapter3

C:\Users\vikas\Desktop\JavaTraining\Chapter3>javac examples\Calculator.java

C:\Users\vikas\Desktop\JavaTraining\Chapter3>java examples.Calculator
The sum is 30

C:\Users\vikas\Desktop\JavaTraining\Chapter3>

The Calculator.java file:

// One Package Statement
package chapter3;
// The file in Chapter 3 folder, file in example folder has
//package examples;
// One or more import statements
import java.io.*;
import java.util.*;

// Class Declaration
public class Calculator {

    // State. Variables and Constants
    int i=10;
    long k = 20;

    // Behavior, one or more methods
     void printSum(){
         long sum;
         sum = i+ k;
    System.out.println("The sum is " + (i+k));
    }
    public static void main (String[] args) {
    Calculator c = new Calculator();
    c.printSum();
    }
}
10
  • Is calculator in a package? Commented Mar 21, 2017 at 13:16
  • The classpath has to be set to the top of the package hierarchy. If it's set to . then you should invoke java from there or set a classpath with the flag cp Commented Mar 21, 2017 at 13:18
  • from package JavaTraining, should be able to do both: javac Chapter3\Calculator.java and java Chapter3\Calculator Commented Mar 21, 2017 at 13:22
  • @LeTex: I thought so, but it didn't work Commented Mar 21, 2017 at 13:48
  • @pvg: the classpath is set to ' . ', can you please elaborate on what you mean Commented Mar 21, 2017 at 13:49

1 Answer 1

1

When you build a file, it is good to have a build directory, then java will put the class in the correct package layout.

mkdir build
javac -d build path/to/source/Files.java
java -cp build package.name.Files
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.