• Post Reply Bookmark Topic Watch Topic
  • New Topic

Frustrating with Could not find or load main class  RSS feed

 
Capstone Dev
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone give me some insight on why this will not run...  Just trying to get back into coding and I cannot even get this simple code to run.

I am using Visual Studio Code as an editor and running the java code from there. 

My JAVA_HOME and Path seem to be set correctly:

echo %JAVA_HOME%
C:\Program Files\Java\jdk1.8.0_131

Here is the directory structure:

\Projects\Java\Bicycledemo
                                     ->Bicycle.class
                                        Bicycledemo.class
                                        Bicycledemo.java

And here is the error output when trying to run the code:
[Running] cd "c:\Users\Projects\Java\Bicycledemo\" && javac Bicycledemo.java && java Bicycledemo
Error: Could not find or load main class Bicycledemo

[Done] exited with code=1 in 1.477 seconds


Here is the code:




 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java command needs to be run either from the Projects\Java folder, or that folder needs to be added to the classpath in the command (using -cp).

The Bicycledemo class is actually the Bicycledemo.Bicycledemo class.  Java uses the full name (which includes packages) for the class.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
That problem shows how frustrating an IDE can be. Lots of people get confused about running code from the command line, but as you have seen, it can be even worse to use an IDE. At this stage, stop using the IDE and stop using the package names. By the way: package names oughtn't to start with CapitalLetters. Particularly when the package name and class name are similar, that is a potent source of more confusion.
 
Anurag Verma
Ranch Hand
Posts: 170
Hibernate Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yet another recommendation would be to declare the class having main method as public.
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not necessary.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!