• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java - Error: Could not find or load main class  RSS feed

 
Adrian McInti
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I'm trying to run a java program from cmd. I'm using Windows 8, java version "1.8_0_20". I've got two classes, the main class called Middleware and a secondary class, called WorkerThread, of which the main class is using functions. I've successfully compiled both .java files with javac, and created the .class file. So all files are in the same folder. However, when I attempt to run the main class with java Middleware, I get the error message: "Error: Could not find or load main class". Here's the main class:



My code compiles and runs fine in eclipse but on the console, I'm having troubles. Could someone tell me what's wrong?

Thank you!
Regards,
-A
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you typing on the command line?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class is in a package named "middleware".

Java requires that your directory structure matches your package structure. That means that the class file must be in a directory named "middleware". You can then run it by making sure the directory that contains that directory is in the classpath, and you need to specify the fully-qualified name of the class (package name and class name separated by a dot) when you run it. For example:

C:\MyProject\middleware contains the file Middleware.class

Run the program with: java -cp C:\MyProject middleware.Middleware

See Lesson: Packages in Oracle's Java Tutorials.
 
Adrian McInti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:What exactly are you typing on the command line?


I was running java -cp Middleware middleware.Middleware

Jesper de Jong wrote:Your class is in a package named "middleware".

Java requires that your directory structure matches your package structure. That means that the class file must be in a directory named "middleware". You can then run it by making sure the directory that contains that directory is in the classpath, and you need to specify the fully-qualified name of the class (package name and class name separated by a dot) when you run it. For example:

C:\MyProject\middleware contains the file Middleware.class

Run the program with: java -cp C:\MyProject middleware.Middleware

See Lesson: Packages in Oracle's Java Tutorials.


Thanks a lot! This finally solved my problem.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Adrian McInti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And welcome to the Ranch


Thanks! Quite a friendly place here.
 
Daniel Siepmann
Greenhorn
Posts: 5
Mac PHP VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I've the same error message.
I used google and http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html.
You can find the code at https://bitbucket.org/DanielSiepmann/tic-tac-toe/src.
It worked using my previous Editor Sublime Text with a package that has it's own configuration settings.sublime-javac.
But now I tried to understand the whole process make and use vim as editor. So you can take a look at the Manifest.txt and Makefile.
Everything compiles fine to the output folder. Even the jar File is created. But I get the error that he can't find the entry class Bootstrap.

Perhaps the issue is that inside the jar File all compiled files are inside the folder output? But I can't figure out how to prevent that.

Perhaps someone has an idea?
 
Daniel Siepmann
Greenhorn
Posts: 5
Mac PHP VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A friend helped me. I've added a cd to the makefile to switch into my output folder. Now it works.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!