Could not find or load main class - for any program - java

I'm having a problem where the java command - no matter what I'm trying to run, says that it Could not find or load main class.
Everything is fine when compiling with javac, .class files are created. So when I run:
javac HelloWorld.java
on
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World");
}
}
everything compiles fine, a HelloWorld.class file is created along side the HelloWorld.java file. However when I then go to run:
java HelloWorld
1) the most telling sign is that when I press Tab to autofill HelloWorld nothing comes up.
2) when I do run it, I get the Error: Could not find or load main class HelloWorld despite it being in the same directory, not being part of a package, compiling fine with a .class file, the program having a main class.
For reference running Fedora 23 64bit, openjdk version "1.8.0_111".

Just a small reminder for the newbies in Java:
When compiling, you type:
javac MyClass.java
Now, you've got two files:
MyClass.class MyClass.java
Now, whereas you typed the .java extension when compiling, you must NOT type the extension .class when running the program. You should just type:
java MyClass
If you type java MyClass.class then you'll get:
Error: Could not find or load main class

Try using java -cp . HelloWorld
Some good reading: http://www.sergiy.ca/how-to-compile-and-launch-java-code-from-command-line/

You need to specify classpath parameter while running your example:
java -cp . HelloWorld

java -cp HelloWorld
works. I use windows 10, and was checking out the very course. I first had to add it to path, and spent time wasted on it. Be sure, however, to NOT include the .class part of the name. Java is not my first language, but Java is portable and is a suitable language for everyone.

The same happened to me while compiling a piece of code (that was initially writen for an IDE with several files) throug terminal. The problem was mentioning the package with the same name of the main class (package HelloWorld). I fixed it and now it works. Not sure if that's your case

May be you have removed your JDK From System.
You can change it using following steps
1> Select Project
2> Right Click On Project
3> Click On Properties
4> Go to Java Build Path
5> Click On Libraries Option
6> Select JRE System Library
7> Click On Edit
8> Change Your Library Accordingly

Related

Importing Classes from another package

Hello so I'm trying to Import a class from another project I made but I cant get it to work..
this is my code:
import program.GUI;
public class name {
//code
}
I get the following error
Opens the new class wizard to create the type.
package: program
public class GUI {
}
I have created a prog02>src>program>GUI.java
How can I solve it.
I think I see the problem here...
I don't know if your running java from cmd, but my explanation is going to assume you are. Outside of using IDEs like Netbeans and Eclipse that pretty much streamline the build process of java code, it's good to know how it works.
Ok, here goes:
1) Compile your java source code files into bytecode (.class files) by invoking JAVAC
javac [optional flags] [path to file intended for compilation]
This creates the bytecode that the JVM will need in order to execute.
2) Invoke the Java interpreter (JVM) to run your bytecode.
java [optional flags] [name of class file w/o .class extension]
If everything goes right this command will create a JVM process with main.java being the main entry to the program that creates the initial thread that runs your program.
Here what you should write to get you program to compile with you package dependencies.
cd to base dir that contains program with main method (src)
javac -cp . program/ (check by going to dir to see if a GUI.class file
was generated)
javac -cp . Main
java -cp . Main
That should do it. Otherwise from and IDE standpoint you either don't have file in the right directory or you are not using the right syntax for specifying your package and import.
The package declaration on the GUI class is not right. It should be:
package program;
public class GUI {
}
You may also find this useful: Java Code Conventions

How to compile and run project with multiple classes in package?

I just realized I'm about to graduate and I still don't know how to handle this situation:
Say I have a java package named mystuff.project1 and inside the package I have Project1.java (which has the main method) and ThingThatDoesStuff.java . Both are public classes, have the package declaration at the top of the file, etc. I can debug this project fine in Eclipse.
For maximum simplicity, I move the project to C:\ so the java files are located in c:\mystuff\project1
I navigate into c:\mystuff\project1 and type javac *.java to compile the class files
Now how the blazes do I run my application?
java Project1 doesn't work
java mystuff.project1.Project1 doesn't work
java -cp . Project1 doesn't work
java -cp . mystuff.project1.Project1 doesn't work
All of the above give me "Error: Could not find or load main class"
I've been searching SO and elsewhere to try to understand this problem but I'm completely baffled.
I've:
cleared my CLASSPATH variable
triple-checked PATH etc
successfully tried compiling and running standalone class files that aren't in a package
I understand this is a stupid newbie question but I just can't figure it out. I realized every other time I've run into this problem I similarly couldn't find a solution and put all of the classes into a single file. I'd rather learn how to stop doing that now.
I think you have to change to C:\ and then enter:
javac mystuff\project1\*.java
java mystuff\project1\Project1
and this should work.
It's either that or:
javac mystuff/project1/*.java
java mystuff/project1/Project1

Running a simple Java Program in Command Prompt error

I am trying to run my Example2.class program in windows 7 64-bit command prompt. I used command prompt already to compile the program, but when type: "java Example2" it gives me an error saying could not find or load main class example2. How do I set the right path to my file so that it can find it? Thanks
You need to give it the full package name, and (unless you change the class path) you need to be in the right directory. If the full package name is
com.something.Example2
then you'd expect the compiler to produce a file like this:
com/something/Example2.class
If you make sure you're in the directory immediately above com (i.e., you can see com when you do a directory listing), then you can run it with
java com.something.Example2
Note that it's case sensitive.
If you used the default package (i.e., the full class name really just is Example2) then you need to be in the directory containing Example2.class, and then you run
java Example2
But using the default package is discouraged.
The biggest thing you could do to help yourself out is to use an IDE (Eclipse or NetBeans are the most commonly used ones). As soon as you start to write anything at all large or complex, compiling and running from the command line without an IDE will cause you to claw your own eyes out.
When you write a class you can save the file as : MyClass.java and then execute this commands in this directory:
javac MyClass.java
which will compile the class and then create automatically the file: MyClass.class (If compilation ended without errors)
and then to run this SPECIFIC class execute the command:
java Myclass

Mac Terminal: Could not find or load main class CLASSNAME

I am trying to run a java program through the Terminal on Mac, yet getting:
Error: Could not find or load main class (MY CLASSNAME)
I compiled this application with Eclipse, and when I run this with Eclipse, it works fine.
Furthermore, I am in the right directory, as when I type "ls" in the Terminal, it lists all the files, includes the class file I am trying to run.
This is what I type:
java mainClass
I would very much appreciate help to solve this!
Thank you,
Dean
EDIT: Solution - instead of java mainClass, it must have package too: java startPackage.mainClass
Start by making sure you are at the directory above the top level package
If the class belongs to the package com.foo.bar, you want to be in the directory above com.
In your case, you want to be in the directory above startPack.
Then you need to use the fully qualified name to run the class...
java statPack.mainClass
For example...
Make sure you have the current directory inside your CLASSPATH.
java -cp . mainClass
To set this globally, you can use export CLASSPATH=$CLASSPATH:. inside .bash_profile.
Separately, if your class lives inside a package such as com.foo.bar, then you will need to go to the parent directory of com and run your application with the full path.
java com.foo.bar.mainClass
I too faced this on Mac machine and then what I had to do to make it work was:
Problem Statement:
I had one package xyz under the root of project i.e src/main/java and then inside xyz package I had one class Student.java
my current directory is /Users/username/projectname/src/main/java/xyz:
I can see Student.java exists here
and I compiled it using javac Student.java
Now I see class file has been created at this location. But when I try to run the class file using java Student
I get the error: Error: Could not find or load main class Student
Solution:
Now the solution is to go one step back in the directory and go to root path:/Users/username/projectname/src/main/java and run the command
java xyz.Student
and it will work.
Link to follow: https://javarevisited.blogspot.com/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html
For people dumb like me, make sure you are typing java HelloWorld - and NOT java HelloWorld.class - to run the compiled file with the name HelloWorld.class. This is especially so if you are used to hitting the tab key to complete the file name, as the terminal will give you java HelloWorld.class if you hit the tab key for autocomplete after typing something like java He...
This answer is here because it took 3 sites, including this answer, and 25 mintues before I figured out what I was doing wrong.
Logic is easy, typing is hard.
Using the absolute path can also resolve this problem:
java -classpath /Users/xingliu/IdeaProjects/springproject/src/main/java/ startPackage.mainClass

java compiling error "could not find or load main class main.java"

I understand there are a lot of threads similar to this one, but I couldn't find the one that solved my problem. Following this instruction I was able to get java in terminal and be able to compile. I am able to "javac main.java" with no errors, but when I "java main.java", it simply says it could not find or load main class main.java. I believe that my classpath is wrong but i'm not entirely sure how to fix this either. This is what comes out when I type in echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/taka/.rvm/bin
and when I type echo $CLASSPATH it doesn't show anything.
I have also tried java -cp ./ main.java as that seemed to have worked when I compiled and ran HelloWorld.java
If your javac is successful then update your classpath environment variable and add current directory i.e. . in the classpath, then run the java as below:
java main
Please note: There is no .java extension as you need to run .class file(which was generated after javac) that also without mentioning the extension. Java uses generated class files to execute not the original source files.
main.java java is your source code . you cant run java source without compile. For compilation you should use javac command. After that it will create a main.class file which can understand by interpreter which is java.
So you to run your class use java main or java main.class

Categories