Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Error: Could not find or load main class <class name>  RSS feed

 
Rahul Dazz Das
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got a public class (MyCal) which is inside a package (mypack) located at E:\Java\ssalc\mypack\MyCal.java; CLASSPATH has been set to E:\Java\ssalc

I am trying to use a method (HCF) of MyCal in an another class (Test) which is located at some different location (E:\Java\mytest\Test.java); refer below code

On compiling (E:\Java\mytest>javac Test.java), the class file (MyCal.class and Test.class) gets successfully generated at their respective location. However program fails to execute (E:\Java\mytest>java Test) & returns following error message: Could not find or load main class Test

Please guide.
 
Anh Giang
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should compile using the -d option so that the classes end up in an appropriate directory hierarchy: javac -d . Test.java and java mypack.MyCal.Test
 
Rahul Dazz Das
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anh Giang wrote:You should compile using the -d option so that the classes end up in an appropriate directory hierarchy: javac -d . Test.java and java mypack.MyCal.Test
Thanks mate!! To sum up, error message was coming because JVM was looking for Test.class file in E:\Java\ssalc\ (as CLASSPATH was set to this location only) whereas it is getting generated at E:\Java\mytest\; Program executing successfully after I compiled using following command: javac -d E:\Java\ssalc Test.java
 
M Shu
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following precautions should be taken care while running java code in command line.

1. Please be sure that you are running program from command line as ADMIN of that PC. (if not, please login from admin account.)
2. Please check whether environment variables are set properly or not.
3. Please store Java Code files only inside the 'bin' folder of JDK directory.

Reason :

(Assume you have stored program on Desktop.)
If you are not logged in as ADMIN, your java files would be stored at location like this..C:\Users\<username_which_is_not_admin>\Desktop
While you run program, compiler looks into admins desktop like this C:\Users\<username_which_is_admin>\Desktop.

This is the reason why your class 'Test' was not visible to JVM.
 
Rahul Dazz Das
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Shu.. wrote:Following precautions should be taken care while running java code in command line.

1. Please be sure that you are running program from command line as ADMIN of that PC. (if not, please login from admin account.)
2. Please check whether environment variables are set properly or not.
3. Please store Java Code files only inside the 'bin' folder of JDK directory.

Reason :

(Assume you have stored program on Desktop.)
If you are not logged in as ADMIN, your java files would be stored at location like this..C:\Users\<username_which_is_not_admin>\Desktop
While you run program, compiler looks into admins desktop like this C:\Users\<username_which_is_admin>\Desktop.

This is the reason why your class 'Test' was not visible to JVM.
Thanks, certainly I had messed up with the environment variable (which has been resolved as mentioned above). However I don't think that java source code / class files are restricted to 'bin' folder of JDK directory only.
 
Campbell Ritchie
Marshal
Posts: 55745
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Shu.. wrote:Following precautions should be taken care while running java code in command line.

1. Please be sure that you are running program from command line as ADMIN of that PC. (if not, please login from admin account.)
2. Please check whether environment variables are set properly or not.
3. Please store Java Code files only inside the 'bin' folder of JDK directory.

Reason :

(Assume you have stored program on Desktop.)
If you are not logged in as ADMIN, your java files would be stored at location like this..C:\Users\<username_which_is_not_admin>\Desktop
While you run program, compiler looks into admins desktop like this C:\Users\<username_which_is_admin>\Desktop.

This is the reason why your class 'Test' was not visible to JVM.

That is nonsense. Very bad advice.
You should avoid logging in to a Windows® box as admin, for security reasons, except when you are doing administrative tasks. Similarly you should only log in as root on a *nix box to perform administrative tasks, and then log out again.
The fact that OP is seeing that particular error message suggests the PATH variable has already been set correctly. the CLASSPATH is incorrect; you should not set a system or user CLASSPATH at all. That CLASSPATH should be deleted, unless it has been set up for that particular command line session only.
You should never store your own code in the bin directory; when you install a new version of Jav®, all your code may disappear.
That is not the reason why the file cannot be found.
 
Campbell Ritchie
Marshal
Posts: 55745
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anh Giang wrote:You should compile using the -d option so that the classes end up in an appropriate directory hierarchy: javac -d . Test.java and java mypack.MyCal.Test
Good advice and welcome to the Ranch. But are we sure the Test class is actually in the mypack package?

Welcome, too, Rahul Dazz Das I am relieved to see you didn't believe the rubbish somebody else told you.

Test is only in mypack if the line
package mypack;
is the first non‑comment part of the .java file. In that case the .class file should be inside the mypack folder and the name of the class will be mypack.Test, so you must run it from the ssalc folder with the instruction
java mypack.Test
as Anh Giang told you. If you do not have that package declaration in the first line, then the class is not in the mypack package; it is in an unnamed package which happens to reside in the mypack folder. Why did you show a directory structure with mytest in? You have two directory structures there, and that bit is confusing.

If you really are in the mypack package, you should be able to remove the import declaration for MyCal. If there is no package declaration, the Test.class file ought to be in ssalc and you can run it from ssalc with java Test

By the way: Test, MyCal, mytest and mypack are really bad names for classes and packages.
 
M Shu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
M Shu.. wrote:Following precautions should be taken care while running java code in command line.

1. Please be sure that you are running program from command line as ADMIN of that PC. (if not, please login from admin account.)
2. Please check whether environment variables are set properly or not.
3. Please store Java Code files only inside the 'bin' folder of JDK directory.

Reason :

(Assume you have stored program on Desktop.)
If you are not logged in as ADMIN, your java files would be stored at location like this..C:\Users\<username_which_is_not_admin>\Desktop
While you run program, compiler looks into admins desktop like this C:\Users\<username_which_is_admin>\Desktop.

This is the reason why your class 'Test' was not visible to JVM.

That is nonsense. Very bad advice.
You should avoid logging in to a Windows® box as admin, for security reasons, except when you are doing administrative tasks. Similarly you should only log in as root on a *nix box to perform administrative tasks, and then log out again.
The fact that OP is seeing that particular error message suggests the PATH variable has already been set correctly. the CLASSPATH is incorrect; you should not set a system or user CLASSPATH at all. That CLASSPATH should be deleted, unless it has been set up for that particular command line session only.
You should never store your own code in the bin directory; when you install a new version of Jav®, all your code may disappear.
That is not the reason why the file cannot be found.



If it would be a non-sense, the OP would not have been given THANK YOU message on that Post.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Shu wrote:

If it would be a non-sense, the OP would not have been given THANK YOU message on that Post.


It was nonsense, though.
I have never needed to log in as Admin.
I have most certainly never put my code int he JDK bin directory (which is completely the wrong place for it).

So that leaves your point 2, which is where the issue was as the OP should not have declared a CLASSPATH environment variable.

The reason Test.class was not visible was entirely down to the existence of that variable.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Shu wrote:If it would be a non-sense, the OP would not have been given THANK YOU message on that Post.

What you described works, but it is bad practice. The OP has thanked you because it worked - but not because it is a good solution.

You should NEVER store your Java source files in the bin directory of the JDK. Instead, create a folder somewhere else on your harddisk to store all the files related to your project. Never put your own files in the JDK directory. It will mess up the JDK and create chaos on your computer.

Also, you do definitely not need to login as admin, and as Campbell and Dave have already said, this is a bad idea and bad advice for security reasons.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!