Error: Could not find or load main class Hello2 - java

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.

Related

Cant find or load main class

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

how to compile and run two different java programs with instances of one in another which are in different directories?

my first java program is
import java.io.*;
class pgm10a
{
public static void main(String args[])
{
pgm10b b=new pgm10b();
b.display();
}
void display()
{
System.out.println("A class");
}
}
it is saved in C:\NNK\pack1
the second program is
import java.io.*;
class pgm10b
{
void pgm10b()
{
pgm10a a=new pgm10a();
a.display();
}
void display()
{
System.out.println("Class B");
}
}
it is in C:\NNK\pack2
I want to run pgm10a but it keeps showing pgm10b not found exception. i have set the class path and compiled for both and both are compiled successfully. but when i try to run them it shows pgm10b not found.
Have a look at the syntax for the java command:
java [options] classname [args]
Anything after the class name is not an option to the java command—it is simply passed as is, in a String array, to the program’s main method.
You can solve your problem by changing your final command from this:
java pgm10a -cp C:\NNK\pack2
to this:
java -cp .;C:\NNK\pack2 pgm10a
The classpath is a sequence of directories, separated by ; when running in Windows (: on other operating systems), which tell the java command where to find compiled classes. If you only specify C:\NNK\pack2, Java will only be able to see classes in that directory. The period (.) refers to the current directory, so the above classpath is pointing to both the current directory (which contains pgm10a) and the pack2 directory (which contains pgm10b).

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

Issues with running a compiled java program

I am a Java beginner. I wrote a quintessential "Hello, World!" program. It compiles, but won't run. The terminal says there is an exception in the thread main, and that the class hello is not found. I am using Ubuntu 12.04. What could be wrong here?
The file is called hello.java.
The commands I used:
$javac hello.java
$java hello
My code is below:
class hello{
public static void main(String[] args) {
system.out.print("Hello");
}
}
EDIT-----------------------------------------------------------------------------------------
I just realized that I am using openjdk7. Does that pose a problem?
class must be: public class hello
system.out.print is wrong, must be: System.out.println("Hello World");
Is the filename hello or Hello? The only way I duplicated your problem was by having the class name wrong, and in java the class name an filename must exactly match (meaning the case too). So if your filename is Hello.java and the class name is hello the program will compile fine but throw the same error you mentioned. This is just a guess though.
Is that all your code? Since you use a terminal, can you add the results of the command ls -lR to your question? (run it in the same directory as your original command executing the application).

How do I run a Java application from the command line when it is in a package? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
package in .java file makes class file unuseable
My Hellow World runs fine.
But as soon as I add a package reference I cannot run it from the command line:
package pv;
public class hcw2 {
public static void main(String[] args) {
System.out.println("Hello Cruel World.");
}
}
compiles fine, then I expect to use java pv.hcw2 to run it, as:
>java pv.hcw2
>Error: Could not find or load main class pv.hcw2
I have also tried just java hcw2, to no avail.
Running in the same directory as the original which runs. Running on Windows 7 64b.
Thank you
You should have a folder called pv under which your file hcw2.java should lie. The folder pv is nothing but your package. Then outside the directory you may issue a javac command as shown below followed by java.
braga#braga-laptop:~$ javac pv/hcw2.java
braga#braga-laptop:~$ ls pv
hcw2.class hcw2.java
braga#braga-laptop:~$ java pv.hcw2
Hello Cruel World.
You have to keep your class in the folder named your package. so for :
package pv;
public class hcw2 {
public static void main(String[] args) {
System.out.println("Hello Cruel World.");
}
}
The hcw2.java should be kept like pv\hcw2.java
once you compile successfully there should be the class file in same folder like :
pv\hcw2.class
While running you have to change directory to the base directory. so if your directory structure like : d:\java\pv\hcw2.java
then
Change dir to d:\java>
Run the java command there with the package name. So :
d:\java> java pv/hcw2 or
D:\java>java test.Test
Your java class needs to be inside a subdirectory called pv. So:
mkdir pv
mv hcw2.java hcw2.class pv
then you can run
java pv.hcw2

Categories