• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

CANNOT load or find main class when i use CMD

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone, when I try to use the command "java classname" it gives me that error, but when i compile it just in Eclipse i don't get that error, the problem is with my cmd,when i go inside the source directory where my project is which is C:\Users\hp\eclipse-workspace\ServerMT\bin>, and type "dir" they list that my class is in there,but when i run the command it just doesn't work,now i somehow have two versions of java which is jdk-13.0.2 and jdk-15, both inside the programs java folder, i really don't know which one of them to use or each one of them works or if that's the source of the problem, i kept updating the environment variables  switching between ones for jdk-15 and one for jdk-13,nothing works, i really don't know how to solve this, been trying to for a week, any help will be appreciated. and when i run the command "echo %CLASSPATH%' i get C:\Program Files\Java\jdk-15\lib*.jar, but  when i type this in search on my pc i can't find the folder, i only find C:\Program Files\Java\jdk-15
Screenshot_313.png
[Thumbnail for Screenshot_313.png]
 
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You can find out which version of Java® you are using with these two instructionsWhy have you got that CLASSPATH variable? I think what is happening is that your OS is looking for the ServerMT.class file in the CLASSPATH rather than the current directory. A system CLASSPATH usually does more harm than good, so try deleting the CLASSPATH altogether. Obviously Eclipse isn't using that CLASSPATH.
It is usually better to copy the text from the command line. Please avoid screenshots as much as possible.
 
jonathan alfred
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've deleted the classpath and it still doesn"t work , and when i run that command what i get is java version "1.8.0_271" but im not sure if thats jdk13 or 15
 
Rancher
Posts: 4749
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please copy and paste from your cmd window rather than posting screenshots.
It makes it easier to copy and paste values.

If the error is  as you say, then does the ServerMT class have a main method?
 
Campbell Ritchie
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jonathan alfred wrote:. . . java version "1.8.0_271" but im not sure if thats jdk13 or 15

Neither nor. It's Java8.
 
jonathan alfred
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code i'm trying to execute is:



so it does have main class and it executes and all,however i've tried using "java -cp . MYCLASS" and it seems to work now for other projects,not this one,i still get error for this one when i run 'java -cp . ServerMT" ,what i get is; java.net.BindException: Address already in use: JVM_Bind
       at java.net.DualStackPlainSocketImpl.bind0(Native Method)
       at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
       at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
       at java.net.PlainSocketImpl.bind(Unknown Source)
       at java.net.ServerSocket.bind(Unknown Source)
       at java.net.ServerSocket.<init>(Unknown Source)
       at java.net.ServerSocket.<init>(Unknown Source)
       at ServerMT.run(ServerMT.java:18)
 
Saloon Keeper
Posts: 22803
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is not Internet Explorer. It is quite permissible and often useful to have more than one version of Java installed on a given computer.

Which version of Java is used depends on the PATH environment variable or, failing that, the explicit filename path (relative or absolute) of the javac and java programs that you type on your command line/batch file.

CLASSPATH is used on the "javac" command to locate third-party classes needed for the compiler to find what it needs to compile. In other words, if you need class com.coderanch.forums.editor.ForumEditor, then you need a classpath that references that file's package directory structure or library JAR. If you're not using any classes that aren't already part of the core Java classes including as part of the JVM, you don't need a CLASSPATH and shouldn't set one.

CLASSPATH is used on the "java" command to locate third-party classes (including your own) needed to execute the application you desire. Generally that would include a set of directories and/or JARs that define the packages and classes used by the app.

And the main method of any Java app MUST be like so: [tt]public static void Main(String[] args)". If the main method isn't public and static, returns anything other than void, isn't properly capitalized, and/or doesn't take one and only one argument (an array of Strings), then the java runtime will not find it and will fail.
 
Campbell Ritchie
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always use the code button. Since you are new, I applied it to your code and doesn't it look better It was very difficult to read before because the indentation wasn't obvious.
You are obviously getting that code to run. Are you trying to run a chat application?
Don't make classes extend Thread. Make them implement Runnable.
You have opened your port and not closed it. Make all your resources inside a try with resources block. What I think is happening is that the port 1004 is still open, so you cannot access it if you run the program again. You may have to reboot your computer to close that port.

I don't like the term “main class,” because it makes it sound important. It is no more important than other classes, but you are using it to start the application. Say, “main method,” instead.
 
jonathan alfred
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys so much! the problem is solved now, and i'll make sure to use the code button next time! i'm just still understanding how this site works ^^
 
Campbell Ritchie
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure Please tell us what the solution was.
 
jonathan alfred
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i used the command java -cp . MYclass instead of java Myclass and it worked! and for the second error i got all i had to do was close the port that was opened as you told me to, and i also changed the complier to 1.8 since it kept saying i'm using a newer version of java to execute
 
Campbell Ritchie
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the information (). That suggests I was right to recommend you remove the CLASSPATH entirely.
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic