It runs successfully now, we have set classpath using -cp option for our java code and my classpath is pointing to D:/myProject where my classfile is present. java -cp D:\myProject frugalis.HelloFrugalis Lets see how we can use CLASSPATH to solve the above issue. Now with CLASSPATH option, we will ask java to look at the class file from other directory. class file with resolved path as E:\frugalis\HelloFrugalis. Now lets run the code from a different directory E:\>java frugalis.HelloFrugalisĮrror: Could not find or load main class frugalis.HelloFrugalisĪgain we are getting the same error, because java is looking in the working directory which is E: in my case, there are no. It actually overrides the CLASSPATH environment variable as well. Well, in order to achieve this you need to set CLASSPATH by either -classpath or -cp option in CommandLine. My Code remains in the same location, but I want to execute the code from a different location. What if We want to Run the Above Code from a Separate Directory let’s say in the above example E: drive instead of D: drive. Now lets tweak the problem a bit More Complex. Follow the book Head First Java, One of the Best book. If you are a beginner and just started your Programming on Java, or someone who is not able to understand java. class and hence the code run successfully. In this is case it is able to locate our. it looks in current directory and resolves it like. ![]() So the reason it works fine now is in java World one we place a. classfile is present, hence we need to move to parent directory and execute the java class. īut we are already inside the directory where. We do not have any CLASSPATH variable due to which it is looking for path as frugalis/HelloFrugalis from current directory. So the problem here is my class is inside non-default package ex:- frugalis in this case. So again we are getting same Error: Could not find or load main class. Now once you have added the file under folder frugalis, it perhaps aligns with the package name and this should run successfully. We are going to create a folder named as frugalis inside MyProject like below. Sounds some issue rite ? Lets try and fix it. We have knowingly defined the package as frugalis in our java code and we are trying to execute it from myProject folder. mkdir myProject could not find or load main classīingo !! We have got the error “Error: Could not find or load main class”. We will now compile and try to run the code from command line. Now move the above java file in a folder named as myProject. So we will now Write a Simple HelloWorld program and replicate the issue, then we will try to fix it. I bet you this troubleshooting that we are doing here to resolve the issue related to loading main class is going to help you in your future, till you are Java developer. How do We fix the Error: Could not find or load main class HelloWorld Now lets come back to the problem where we get Could not find or load main class. So now java CLASSLOADERS scans through the classpath to load your classess. Have a Look at few Best Spring Courses for Java Developers in 2021Īs IDE itself manages all the CLASSPATH related stuff internally, but while you start running the same code from your terminal in linux or windows command line you will actually face this issue due to CLASSPATH. In eclipse or IntelliJ if you get this error you will not be able to understand the reason quickly if you are new java developer. It is easier to run and debug java code in IDE than command line. So new Java developers uses lot of IDE like eclipse and intellij to run and debug Java code. class file is not found on CLASSPATH, java will throw this error : could not find or load class Main. So for Understanding CLASSPATH is basically a part of our environment variable, While running the Java file if the corresponding. For our understanding we won’t go deep into CLASSPATH and class loading. In order to fix this error you need to have clear understanding of How Java class loading Works and working of CLASSPATH. This issue is seen mostly when java is not able to find the class it is trying to execute in your CLASSPATH. We have a HelloWorld.java class and I am trying to run the code from command line. What does this error mean could not find or load main class in java code. It is one of the most common issue newbie developers face while starting with java programming.
0 Comments
Leave a Reply. |