Cant find or load main class - java

When I try to run a java program in the cmd it displays the messege "Could not find or load main class".
The problem occurs when there is a package involved in the program, otherwise it works fine.
The code is:
package myPackage;
public class index {
public static void main(String [] args){
System.out.println("Hello World");
}
}
I've tried writting in cmd: javac (name of package) . Class name, but still doesnt work.

The issue was that the class path needs to be set for each command (javac and java):
Attempted Steps
Compile index.java from the top_level. do not use sub package.
$javac -cp . importpackage/subpackage/index.java

Related

Could not find or load main class in a simple java project

I have created a simple java project "DesignPatterns".
I created a file FacadePattern.java with the path being ~/DesignPatterns/Structural/FacadePattern/FacadePattern.java
My FacadePattern.java class,
public class FacadePattern {
public static void main(String args[]) {
// Some code
}
}
But my editor (VSCode) gives me an error at the start of the file:
The declared package "" does not match the expected package "Structural.DecoratorPattern"
So upon clicking on quick fix, it added package Structural.FacadePattern; in the first line.
So the final code became
package Structural.FacadePattern;
public class FacadePattern {
public static void main(String args[]) {
// Some code
}
}
But when I compile the above file using the command
javac FacadePattern.java && java FacadePattern
It is giving me the following error:
Error: Could not find or load main class FacadePattern
Caused by: java.lang.NoClassDefFoundError: Structural/FacadePattern/FacadePattern (wrong name: FacadePattern)
After searching a lot in the internet, I ran the following command:
javac -sourcefile ~/DesignPatterns FacadePattern.java && java FacadePattern
But no use, I am getting the same error.
Can anyone explain why my editor gave an error but code ran successfully before? and why wont it compile after adding "package Structural.FacadePattern;" line? and what is -sourcefile parameter in javac command? and how to run the code with successfully without errors in editor as well as the terminal?
Stick to naming rules. Package names should be lowercase only. In your case, it should be package structural.facadepattern;
Run the 'correct' class. Because your class is not inside 'the base folder' aka the 'default package', you have to prefix the class with its package name: java Structural.FacadePattern.FacadePattern or rather java structural.facadepattern.FacadePattern if you use the proper case for packages.

Error: Could not find or load main class Test in command prompt

I used Edit plus to write the code:
import java.lang.System;
import java.lang.String;
class Test
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
After compiling it in command prompt with javac and executing with java commnds,
I am getting "Error: Could not find or load main class Test".
It was working fine in eclipse.
I even created path and classpath. But didn't work.
C:\Users\sss>cd C:\Users\sss\Desktop\Siri Study\java prog\ratanJprog
C:\Users\sss\Desktop\Siri Study\java prog\ratanJprog>javac Test.java
C:\Users\sss\Desktop\Siri Study\java prog\ratanJprog>java Test
Error: Could not find or load main class Test
Please help me. Thanks in advance.
Where you have written your code, go to that folder and type cmd in search bar see here and execute your pgm from there. I had faced same problem but that was solution for me.
you should change the class modifier to public

How do I compile my main Java program on the command line when it includes an external java package?

I'm a relative Java newbie so apologies if the question appears somewhat basic. I've googled high and low for an answer here and I'm not finding anything that's helping.
Problem:
Whilst I'm able to integrate external packages into my Java programs from an IDE environment, I am trying to do run a very basic program from the command line that calls on a separate, basic package file that I have written - and am simply doing all this as I want to have a bottom-up understanding of how package files are related to a main program by Java.
I have a program that sits on my desktop named MyProgram.java:
import org.somepackage;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
Still on the Desktop, I then have another folder which I've named src, inside of which I have created the necessary subfolders corresponding to the package name, i.e. ./src/org/somepackage - and in this location, I have the Human.java file which defines the Human class with the following contents:
package org.somepackage;
public class Human {
public void scream() {
System.out.println("I exist!!");
}
}
I then created a classes folder, again on the Desktop, and ran the following compile command on the command line:
javac -d ./classes/ ./src/org/packagename/Human.java
This ran fine and created - as expected - the Human.class file within the ./classes/org/packagename/ location.
However, where I fall down is when I then try to compile MyProgram.java on the command line, i.e.
javac -cp ".:./classes/" MyProgram.java
As you'll see, my class path contains a reference to the current location (".") for the MyProgram.java file, and it contains a reference to the classes folder ("./classes/") which is the base location for the org.somepackage package inside whose subfolders (./classes/org/somepackage/) on can find the Human.class file.
At this stage, I was simply expecting the java engine to compile MyProgram.java into the program MyProgram.class - but, instead, I get an error:
MyProgram.java:1: error: package org does not exist
I've been following the instructions listed here:
https://www3.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html
and I don't appear to be deviating from the instructions - yet I'm unable to locate an explanation on Stackoverflow or anywhere else as to a possible reason for this compile failure. If anyone has an idea, your help would be very much appreciated.
Thanks,
Your mistake is here
import org.somepackage; <--
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
you forgot to import class actually, you need to write this name
import org.somepackage.Human; import all package content import org.somepackage.*; or write full qualified name of class in your code
org.somepackage.Human myHuman = new org.somepackage.Human();
myHuman.scream();
correct mistake:
import org.somepackage.Human;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
after that compile your Human.java by this command:
javac -d classes Human.java
and MyProgram.java
javac -d classes -cp "classes" MyProgram.java
and run MyProgram by
java -cp "classes" MyProgram

Simple Java code gets "Could not find or load main class JavaBatchInserter.class"

I have:
public class JavaBatchInserter {
public static void main(String []args) {
System.out.println("Hello World");
}
}
I go to the pwd where my file, named JavaBatchInserter.java, is located and run:
java JavaBatchInserter.java
and get:
Could not find or load main class JavaBatchInserter.java
It's the simplest thing and I can't make it work. ): Help please! Thanks!
Note: I don't want to create a project and a bunch of directories if possible.
Firstly, compile the file JavaBatchInserter.java
javac JavaBatchInserter.java
Then give your full class name to JVM for execution
java JavaBatchInserter
You run the command java JavaBatchInserter.java
It is wrong.First compile the class by running javac JavaBatchInserter.java
The try to run by java JavaBatchInserter

Error: Could not find or load main class Hello2

I'm just learning java and following a book.
I have a program written via text editor and run commands via cmd.
I've complied 1 program thru javac and executed thru java no problem. (Hello)
Then I modified that to add a comment to the class, named file Hello2.java. I compiled it with no problems, but upon execution, I receive this error: Could not find or load main class Hello2.
I have classpath and path set correct;y on environment variables.
Ideas?
UPDATE
Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Hello2.java
//Filename Hello2.java
//Written by
//Written on
public class Hello2 {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
/*This class demonstrates the use of the println() method to print the message Hello, world! */
I've found the solution to my problem. I know it's not a code problem. But what I did is that I deleted CLASSPATH from system variables and everything now works...at least for now.
Thanks a lot everyone for your inputs, much appreciated!
You have to change the name of the public class too when you change the name of the file. So, if your file is called Hello2.java, the class should be called Hello2 and not Hello.
Are you sure you set the classpath correctly? Why don't you try running java -cp the directory of the .class file Hello ? If that doesn't work please upload the full stacktrace.
You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath.
Note that the windows classpath separator is a semi-colon ie ;
If your class file is saved in following directory with Hello2 program name
d:\sample
java -cp d:\sample Hello2
java -cp . Hello2
I believe you have Hello2.java file as below.
class Hello {
public static void main (String args[]) {
System.out.println("Hello");
}
}
Change it to
class Hello2 {
public static void main (String args[]) {
System.out.println("Hello");
}
}
The change is class Hello2 instead of class Hello.
Note : You should always have classname and file name SAME.
Good Luck!!!
Update 1
Are you doing below steps??
Wrote Hello.java
Compile by javac Hello.java
Run by java Hello
Rename Hello.java to Hello2.java
Rename class name i.e. class Hello to class Hello2
javac Hello2.java
java Hello2
I believe you are missing Step 6 & executing step 7 after step 5. Please confirm.

Categories