• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"Error: Could not find or load main class Main" - problem

 
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, Guys,
I am stuck, tried different things and nothing is working.
I compiled java file Main.java
it created Main.class file.
Now I try with java Main and it does not work. I receive the message "Error: Could not find or load main class Main".
Please, help. What should I do?
 
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Your Main class probably belongs to a package, and your ran the java command from the wrong folder without specifying a class path.

Please show us what you Main class looks like, the directory structure of your project, the commands you used to build and run your application, and tell us from what working directory you ran the commands.
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thank you.

it's:

public class Main {

   public static void main(String[] args) {
       System.out.print("Hi, ");
       System.out.print(args[2]);
       System.out.print(args[1]);
       System.out.print(args[0]);
       System.out.println(". How are you?");
   }

}

I wrote in cmd:

javac Main.java
Main.java

 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and the java  and class files are in the folder:

C:\Users\Glopn_23\IdeaProjects\project3\src\com\company
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your directory structure suggests that your main class is part of a package called com.company, yet the source you posted doesn't contain a package statement.

You haven't told us the working directory from which you are running the commands.

You didn't show us how you call the java command.
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Your directory structure suggests that your main class is part of a package called com.company, yet the source you posted doesn't contain a package statement.

You haven't told us the working directory from which you are running the commands.

You didn't show us how you call the java command.



Sorry, I am new. I don't know what is the working directory and what do you mean by how I call the command?
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the following. From the project3 directory, run the following commands:
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Andrew-Mike wrote:I don't know what is the working directory


It's the directory you are in when you run the commands. Your command prompt shows you what directory you are currently in.

what do you mean by how I call the command?


Your previous post doesn't show java, only javac.
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Try the following. From the project3 directory, run the following commands:



What do you mean by "from the project3 directory"? I thought I could run the program from cmd.
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Try the following. From the project3 directory, run the following commands:



I put the first in cmd and received: "javac: file not found: src\com\company\Main.java
Usage: javac <options> <source files>
use -help for a list of possible options"
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. But CMD has a "current working directory". It's the currently selected directory, and it's shown to you by the command prompt. The current working directory matters to the behavior of the commands you execute.
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Andrew-Mike wrote:I put the first in cmd and received: "javac: file not found: src\com\company\Main.java


Because you didn't run the javac command from the project3 folder.
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Yes. But CMD has a "current working directory". It's the currently selected directory, and it's shown to you by the command prompt. The current working directory matters to the behavior of the commands you execute.



So how am I supposed to change the directory? Sorry for being so stupid, but I fight with it since the morning and it's driving me crazy.
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

John Andrew-Mike wrote:I put the first in cmd and received: "javac: file not found: src\com\company\Main.java


Because you didn't run the javac command from the project3 folder.



but the javac I did myself worked. it created Main.class. so why doesn't it run?
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can change the directory using the cd command. For more details, google "cmd cd".

An easy way to start a command prompt from the intended working directory directly is to browse to the intended working directory using your file explorer, and when you're inside the directory enter "cmd" in the "address bar", replacing the directory path that was there. When you hit enter, it will start a command prompt.
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Andrew-Mike wrote:but the javac I did myself worked. it created Main.class. so why doesn't it run?


Because you're in the wrong working directory and didn't specify a classpath.
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

John Andrew-Mike wrote:but the javac I did myself worked. it created Main.class. so why doesn't it run?


Because you're in the wrong working directory and didn't specify a classpath.



Ok I changed the directory. and still the same problem, nothing changed

C:\Users\Glopn_23\IdeaProjects\project3\src\com\company>
C:\Users\Glopn_23\IdeaProjects\project3\src\com\company>javac Main.java

C:\Users\Glopn_23\IdeaProjects\project3\src\com\company>java Main
Error: Could not find or load main class Main

I tried also:

C:\Users\Glopn_23\IdeaProjects\project3\src\com\company>javac -sourcepath src -d bin src/com/company/Main.java
javac: file not found: src\com\company\Main.java
Usage: javac <options> <source files>
use -help for a list of possible options
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, your current working directory needs to be the project3 folder.

The src/com/company/Main.java path that you pass to the javac command is relative to the project3 folder, so that's why that folder needs to be the current working directory.
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Again, your current working directory needs to be the project3 folder.

The src/com/company/Main.java path that you pass to the javac command is relative to the project3 folder, so that's why that folder needs to be the current working directory.



still the same.

C:\Users\Glopn_23\IdeaProjects\project3>javac Main.java
javac: file not found: Main.java
Usage: javac <options> <source files>
use -help for a list of possible options

C:\Users\Glopn_23\IdeaProjects\project3>java Main
Error: Could not find or load main class Main
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Again, your current working directory needs to be the project3 folder.

The src/com/company/Main.java path that you pass to the javac command is relative to the project3 folder, so that's why that folder needs to be the current working directory.



I tried also this :

C:\Users\Glopn_23\IdeaProjects\project3>javac -sourcepath src -d bin src/com/company/Main.java
javac: directory not found: bin
Usage: javac <options> <source files>
use -help for a list of possible options
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read my instructions carefully.

Stephan van Hulst wrote:Try the following. From the project3 directory, run the following commands:

 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Andrew-Mike wrote:javac: directory not found: bin


This means you must make an empty bin folder in the project3 folder.
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

John Andrew-Mike wrote:javac: directory not found: bin


This means you must make an empty bin folder in the project3 folder.



Ok. I created the bin and it helped with the first command (since it shown no error), but still the program does not run and it can't find it.

C:\Users\Glopn_23\IdeaProjects\project3>javac -sourcepath src -d bin src/com/company/Main.java

C:\Users\Glopn_23\IdeaProjects\project3>java -cp bin com.company.Main
Error: Could not find or load main class com.company.Main
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Please read my instructions carefully.

Stephan van Hulst wrote:Try the following. From the project3 directory, run the following commands:



I do everything you tell me, really!
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you tell us what's inside the bin/ folder? Is there a Main.class inside the bin/ folder, or is there a com/company/Main.class file in the bin/ folder?
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Can you tell us what's inside the bin/ folder? Is there a Main.class inside the bin/ folder, or is there a com/company/Main.class file in the bin/ folder?



Stephan, that was the case. The file is Main.class, so I changed it in the command and it works! I just almost don't believe it
Thank you so much for your great help and patience. I really appreciate that.
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hold on. I'm glad it worked, but it's not correct.

It's very likely that the src/ folder acts as a root folder for your package hierarchy. This means that the Main class must be part of the com.company package. The fact that Main.class is output in the bin/ folder directly means you haven't declared it as being inside the com.company package.

While it might work now, you will run into problems as soon as your Main class starts referencing other classes.

The solution is to put the following statement at the top of your source file, and then use my original java command to run the application:

Double-check that Main.class is output in bin/com/company/.
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Hold on. I'm glad it worked, but it's not correct.

It's very likely that the src/ folder acts as a root folder for your package hierarchy. This means that the Main class must be part of the com.company package. The fact that Main.class is output in the bin/ folder directly means you haven't declared it as being inside the com.company package.

While it might work now, you will run into problems as soon as your Main class starts referencing other classes.

The solution is to put the following statement at the top of your source file, and then use my original java command to run the application:

Double-check that Main.class is output in bin/com/company/.



Ok, I put the statement in the source, did everything again and works perfectly. Thank you, Stephan, your my today's hero. Thank you.  
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good on ya for sticking with it. Have a cow!
 
John Andrew-Mike
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Good on ya for sticking with it. Have a cow!



Cool! Thanks!
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't followed thhis thread closesly, but different files in /src and /bin folders suggests, maybe, you are using the folder structure for an IDE from the command line. The two often don't go well together.
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I haven't followed thhis thread closesly, but different files in /src and /bin folders suggests, maybe, you are using the folder structure for an IDE from the command line. The two often don't go well together.


I suggested that. I don't see why it wouldn't work well together.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need a much more complicated classpath to get your app to work; as we have seen it cna throw people not familiar with the problem.
 
Stephan van Hulst
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that "-cp bin" is that complicated, not even for beginners. It also makes them familiar with keeping their source code and compiled classes in different folders, which makes your project easy to clean (you just have to clear out the bin folder) and it also makes them more familiar with how build tools like Maven operate.

If anything, it kinda bothers me that the -cp and -sourcepath options aren't taught to beginners on the very first program they compile.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are right, Stephan, and I was mistaken.
And yes, -cp is like Streams: something all beginners should learn maybe not with their first program, but certainly within a few weeks of starting
 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic