Java can't find or load main - java

I have recently installed canopy (i don't know if this has anything to do with java)
i have set to system variables the path to javac and java
i was writing compiling and running java programs normally until now that it keeps telling me it can not find or load main class.class
example of code:
class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
what is happening?
ok i changed hello to Hello
i run at powershell:
javac Hello.java
java Hello.class ( i am in the directory of the file)
Error: Could not find or load main class Hello.class

You need to run your program using
java Hello
and omit the .class extension

That should compile fine which Java compiler are you using? Make sure to always save and compile your program before running it.

Related

Error: Could not find or load main class ( in Java 8)

I am trying to learn Java and I've made my first program and compiled it into a class file (the file is called aye.java and when compiled I have aye.class, I think the compilation worked). However when I use the java command in the folder where the class is located it just returns below error -
Could not find or load main class aye.class.
I have tried including the package name (com.java24hours) but it still doesn't work.. please help!
Commands I have tried:
java aye.class
java com.java24hours.aye.class
java aye
java com.java24hours.aye
program code:
package com.java24hours;
class aye {
public static void main(String[] args) {
//java code yeet
String aye = "Hello World!";
System.out.println(aye);
}
}
(I am running Linux on a Chromebook and have installed Java via the ppa:webupd8team/java)
Thanks.
I suppose you wanna put binaries to ./bin folder.
Compile aye.java:
javac -d ./bin aye.java
Then cd to ./bin directory and run the program:
cd bin
java com.java24hours.aye
well im stupid
since im new to java, i didn't know anything about packages and such. turns out all i had to do was put the class file in a folder named "ya" (that's the name of the package - i updated the program) and run the command
java -cp /home/ramsey/Documents/ya aye
(-cp stands for classpath, and you use it when you want to specify where you class is located MAKE SURE TO PUT IT IN A FOLDER NAMED AFTER YOUR PACKAGE!!!)
the wiki page is helpful: https://en.wikipedia.org/wiki/Classpath_(Java)
its under the section setting the path to execute java programs
thanks for the help everyone!

Error: Could not find or load main class in Textpad 8

I've been trying to get back into programming and I've been redoing some old labs. I'm setting up Textpad 8 so I can run java applications and it works fine until I add a package statement like so:
package pkg;
public class inPkg{
public static void main(String args[]){
System.out.println("Hello World");
}
}
The file's location: C:\214\pkg\inPkg.java
When I compile everything is fine but when I try to run it, I get this error message:
Error: Could not find or load main class inPkg
Tool completed with exit code 1
Compile Java Tool:
Parameters: javac -classpath "$FileDir;h:\214\;c:\214\;" $File
Run Java Application Tool:
Parameters: java -classpath "$FileDir;h:\214\;c:\214\;" $BaseName
These tools are the only thing I changed in the configuration. The classpath have been written to follow the lab. instructions.
PS. Without the packages statement, the application runs perfectly.
Because probably you are not using the fully qualified class name when you do the java run. use
java -classpath 'your class path' pkg.inPkg
It will compile and execute correctly with following commands
C:\214>javac.exe pkg\inPkg.java
C:\214>java.exe pkg.inPkg
Note that the file location is C:\214\pkg\inPkg.java, however you execute the commands from C:\214

Could not find or load main class

Since 2 days now I am trying to run the simplest program and I can't.
I run my programs from Windows cmd prompt.
Program:
public class Bla {
public static void main(String [] args) {
System.out.println("works");
}
}
Saved the source code as Bla.java.
Compiled the program with javac Bla.java --> Bla.class created. Tried to run the program with java Bla.class
I get the error:
"Could not find or load main class Bla.class"
I am not a complete newbie with java
1. I have configured my path and my classpath variable (Exact values below). Path: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\system32\wbem;C:\Python27;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\Skype\Phone;C:\mysql\bin;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI\wbin;C:\Program Files\Java\jdk1.8.0_144\bin
CLASSPATH: .;C:\Program Files\Java\jre1.8.0_144\lib;
2. my program doesn't belong to any package and doesn't call any package
I call everything from my command line. I tried uninstalling and reinstalling java developer kit.Every time I get the same error. This is not the first time I have installed java or run a program in java but I haven't written something in a long time. What am I doing wrong?
Thanks
Assuming your working directory containes Bla.class, and your CLASSPATH contains a "." (among others), java should be able to find it.
However, you should call it without ".class", like this: java Bla. Otherwise, java will think you want to call a class called "class" in package "Bla".

javac -cp unable to find or load main

Whenever I compile the java package through this code
javac -cp ch03.stacks StackDriver.java ArrayStack.java BoundedStackInterface.java StackInterface.java StackUnderflowException.java StackOverflowException.java
It compiles correctly but when I use this command on the driver class
java StackDriver
It responds with this error
Error: Could not find or load main class StackDriver
Why can't I run the main class in the Driver? And what should I do in order to run the main class?
Figured out that in order to run my Driver I must use this command outside the folder
java ch03/stacks/StackDriver
Which is a bit stupid but I won't dwell on it.
Also discovered a very useful way to compile all of the packages in a folder
javac ch03/stacks/*.java
I will most definitely remember this screw up and learn from it.
JVM cannot find your main() method. You are probably missing this line of code:
public static void main(String args[]) {
//code here
}

Executing error in java

The principal class XXXX has not been found
The code constructs perfectly but when I try to execute it I get the same error again.
I type the following "Hello world" code:
public class Hello {
public static void main(String[] args) {
System.out.println("Hola mundo");
}
}
I get the same error.
It must be a problem of my program ,I use GEANY because my teacher told me that I can't use netbean or similar.
How do you compile and execute your code? Is it some IDE (Eclipse) or just command line?
IDE should take care about this issue. In command line you have to include all classes into the classpath. Sometime it is confusing because when you compile your code mutually is in classpath but you have to mention it litreally to run.
Here is example
javac Hello.java
compiles Hello.java source to the Hello.class code to the current location. Literally ./Hello.class
To run you should mention this location
java -classpath . Hello
without -classpath . java doesn't know where your class is.

Categories