Error: Could not find or load main class. Command Prompt - java

I am trying my had at java programming and I made a HelloWorld program and if you need the code its right here:
public class Main {
public static void main(String[] args){
System.out.println("helloworld");
}
}
It compiled without error and whenever I try to run it from the command prompt it says:
"Error: Could not find or load main class".
I don't know what I'm doing wrong. I have the jre 1.8.0_45 and jdk 1.7.0_71.

As per your comment
I navigate to the location of the .class and type "java
helloworld.class" – Takdm
you don't .class when you run the program. Just do
java helloworld

First- the file name and the class name must be identical.
Then compile your program using javac FileName.java command
To run your program try- java -cp . ClassName
Should work.

Adding . to CLASSPATH variable in Environment variables solved my problem!!
Java searches for the classes in the paths mentioned in the CLASSPATH variable if you don't add . there, it won't search for classes in the current working directory!!!
Go to Control panel > System and Security > System > Advanced System Settings > Advanced
Click Environment Variables
If the CLASSPATH variable is present under User variables, add '.' separated by a semicolon. For example, if Java is installed in C:\Program Files\Java\jdk1.8.0_131\bin , CLASSPATH will be
C:\Program Files\Java\jdk1.8.0_131\bin;.;
Click OK.

Related

Error: Could not find or load main class xxx Linux

I am very new to linux environment.
I am trying to run an simple hello world java class in linux environment.
Hello .java
package com.util;
public class Hello {
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("hi");
}
}
I have compiled java class in windows environment and uploaded the .class file to linux system into /home/scripts path.
my command is as follows,
java -cp /home/scripts com.util.Hello
when i am executing this command from this same /home/scripts where Hello.class is there i am getting,
Error: Could not find or load main class com.util.Hello and not able to proceed further.
can some one help me in this issue?
navigate to /home/scripts using terminal
javac com/util/Hello.java
then
cd /home/scripts
java -cp . com.util.Hello
Or,
java -cp "/home/scripts" com.util.Hello
At first you must generate your .class file :
javac ./hello.java
This command has generated hello.class file
And after you can run your class file ! :)
java hello
We first know javac command work well.
I also met this error,and i have resolved this.Let me share this.
First we need to find the parent path of your package in your java codes.
Then cd to that path using java package + fileName should work well at that moment.
I had the exact same issue on windows, and I solved it by adding path "." to both CLASSPATH and PATH, maybe you can try this on Linux as well.
Your .class file should not reside in /home/scripts/, but in /home/scripts/com/util/. Take a look at this document that explains the relation between classpath, packages and directories.
Before Specifying the path,ensure you follow these three things meticulously,
1. Close the command prompt window, before specifying the path.
2. When adding path, add bin and semi- colon at the end and
3. If JAVAC command has worked properly, try java -cp class name.
if you want to run program in current working directory where your class reside.
java gives three options.
first option
java -cp Tester
Second option for current working directory
java -cp . Tester
Third option export CLASSPATH variable
export CLASSPATH=$CLASSPATH:. (this is the best one if your directory changes) or
export CLASSPATH=$PWD
or
export CLASSPATH=
after that you must sorce the bashrc or bashprofile.

java runs in eclipse but won't run in command prompt

Just today I noticed that I can run java in eclipse with no problems but when I try to run it in the command prompt, I get "cannot find or load main class." The command prompt actually compiles all right, and it outputs a .class file, but then it displays the error msg when trying to execute. (Also, I was able to run java in the cmd a couple weeks ago.)
/* work area for practice
*
*/
package Scrap;
public class experimentational {
public static void main (String [] args) {
System.out.println("welcome to java!");
}
}
Found the answer: (i'm using different code but it is still relevant to this problem)
java -cp . hiThere
output: "Hi there"
I know this is classpath but don't know why it works or what the period does for it. Anyone have an idea?
Use:
javac Scrap/experimentational.java
followed by:
java Scrap.experimentational
try java -cp . [your main class].
Did you install a JDK on the machine outside of Eclipse? If you did, then make sure you set your path variables correctly. Open a command prompt (assuming windows) and type java -version
If the JDK was installed properly and path variables were set properly it should tell you the version of Java that was installed. If it tells you that 'java' is not recognized as a command that you do not have a JDK installed, or it was not installed properly.
The reason your program runs in Eclipse is that Eclipse for Java has its own internal JDK and JVM.
Your other option is to set up your path variables to point to Eclispe's internal JDK.
If you were able to run it from a command prompt previously then most likely your class path was altered. Is this a machine at work? Some companies have SMS tasks that come through periodically and restore default system settings (including path variables) to corporate defaults.
Maybe java and javac isn't in your OS path.
If you are using Microsoft Windows in cmd type path and then enter.
If jdk or jre isn't in path you need to put them to it
I had a similar issue when I copy pasted code into an editor. I removed the package declaration on line 1 and it ran then. So I'd investigate above comments on packages, after trying first to remove the package line.

Running Java Programs on the Command Prompt

I have done a lot of researching on this concept but I can't seem to run a java program on the command prompt. Let's say we had a simple program like this:
public class Hello_World {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
On the command prompt I tried:
javac Hello_World.java
But I get:
'javac' is not recognized as an internal or external command, operable program or batch file
So I compiled it on BlueJ and then did this:
java Hello_World.java
But it said "cannot load or find main class Hello_World"!
I am currently using Windows 7, and made the programs on Notepad++ and BlueJ (to compile).
Any suggestions? Thanks!
This explains in detail what you have to do to set class path. Primarily you need to set your environment variables so that your shell finds the right directory containing javac to compile your program
javac' is not recognized ..
comes when you haven't point your java bin directory to your path environment variable. Because bin directory is the place where javac.exe exist.
To do it.
1) right click on mycomputer property
2) go to Advance system settings.
3) go to environment variable.
4) In system variable click on path
5) go to edit mode and provide your path to java bin directory.
in my case it is C:\Program Files\Java\jdk1.7.0_01\bin;
'javac' is not recognized as an internal...
means OS does not know where javac program is located. Either add it to PATH or run explicitly
my\path\to\file\javac Hello_World.java
Compiling will convert *.java to *.class
Hello_World.class file should be located according to it's package directive. Since you have no one, in your case it should be located in the same directory you will run java.
To run your class specify it's name not file name
java Hello_world
looking for the class is essential part of launching and occurs by rules.

compile .java through command prompt

I'm new in programming, I'm learning Java right now. I tried to use the javac command, but the environment says that javac is an unknown command.
How to use "javac" to compile .java files from the command prompt? I'm using eclipse 1.2.2.20100216-1730, JRE 1.6 and JRE6
The JRE has the "java" program for running programs already compiled. The "javac" program is only in the JDK. Download and install the JDK. If BTW it still gives you the same error, then you need to add the javac directory to your PATH environment variable.
Before the Java virtual machine (VM) can run a Java program, the program's Java source code must be compiled into byte-code using the javac compiler. Java byte-code is a platform independent version of machine code; the target machine is the Java VM rather than the underlying architecture. To compile a Java source code file add.java, you would do the following:
javac add.java
If there are no errors in your source file, the Java compiler will produce one or more .class files (one .class file for each class defined in the add.java source file). For example, the results of a successful compile of Foo.java will produce a byte-code version of the class in a file named Foo.class.
Every public class that you write must be in a separate .java file where the first part of the file name is identical to the class name. The .java file additionally can contain code for protected and private classes.
Once you have successfully compiled your Java source code, you can invoke the Java VM to run your application's byte-code:
java <class with main method to run> [<command line args>, ...]
For example, to run the main method from the Foo class:
java Foo
Any command line arguments (arguments to add's main method) follow the class name:
java add 10 20
Such Error may occur due to two reasons:
You have not installed java jdk on your system.
You have not set the environment variables.classpath ,path.
Setting Path and classPath:
Windows XP
Select Start, select Control Panel. double click System, and select the Advanced tab.
Click Environment Variables. In the section System Variables, find the PATH environment variable and select it.
Click Edit. If the PATH environment variable does not exist, click New.
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Windows Vista:
From the desktop, right click the My Computer icon.
Choose Properties from the context menu.
Click the Advanced tab (Advanced system settings link in Vista).
Click Environment Variables. In the section System Variables, find the PATH environment variable and select it.
Click Edit. If the PATH environment variable does not exist, click New.
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
If you have not set the classpath and path you can access javac giving full path:
such as C:\Java\jdk1.7.0\bin\javac MyClass.java
To check the path and classpath, type these commands in a command window:
echo $PATH
echo $CLASSPATH
If you get a blank command line in response to either of these, then that particular variable has no value (it has not yet been set).
setting path and classpath through cmd:
set path=c:\j2sdk1.4.1_01\bin(Give the path of bin)
set classpath=;(or the directory where you want your class files)
Download and install JDK
set environment path --> edit path in environment path and add ;/bin
using javac command --> javac *.java or javac ClassName.java
When run the main method you should note to [package] name
java packagename.ClassName

I have managed to compile java-program but I cannot execute it

I have just installed JDK on Windows Vista. After that I set proper values for the 4 environment variables: classpath, include, lib, path. After that I was able to compile my HelloWorld-program (I got a *.class file). But when I try to execute the compiled program (I type java HelloWorldApp) it does not work. The Java write a lot of stuff and in the end it is written that it "could not find the main class: HelloWorldApp". Can anybody, pleas, help me with this problem?
Just for clarity; you are saying that you have a class in the default package, that is you have not included a package specifier in the Java file, and your class is called HelloWorldApp. When you compiled this, you got a classfile HelloWorldApp.class in the current directory.
Assuming the above to be true then try:
java -cp . HelloWorldApp
For example, the following works on a unix box:
$ echo 'class HelloWorldApp { public static void main(String []argv) { System.out.println("Hello World!"); } }' > HelloWorldApp.java
$ javac HelloWorldApp.java
$ java -cp . HelloWorldApp
Hello World!
Of course, you should indent your code a little nicer than just shoving the whole thing onto one line ;-)
Edit: To answer the comment:
Normally, the default classpath is the runtime libraries and the current directory. However, if you have the CLASSPATH variable set, then this will override the default, and you need to explicitly set the classpath back to its "default value". To verify if the CLASSPATH environment variable is set, you can do (again assuming unix):
set | grep CLASSPATH
If it is set, that is why you need to manually include . on your classpath.
create a file called HelloWorld.java;
paste the code posted below inside HelloWorld.java:
compile it by executing the command: javac HelloWorld.java in the same folder as HelloWorld.java is in;
execute the code by doing: java -cp . HelloWorld in the same folder as HelloWorld.java is in.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("HelloWorld works!");
}
}
How the classpath works, can be read here: http://en.wikipedia.org/wiki/Classpath_%28Java%29
Have you included . and .. in your path? Just for clarification . represents your current directory and .. represents your parent directory. You are telling that the java has to search the current directory and the parent directory to find the class. Add the same to your classpath too.
What happens if you use:
java -cp {path to directory with HelloWorldApp in it} HelloWorldApp
That path should be contained within your CLASSPATH environment variable. Is that exported to your command shell ? Do you need to start a new command shell to get the most recent version of CLASSPATH ?
Post your code. I believe the problem is that your main class is not defined properly. I did this the other day.
public static void main(String[] args){
//code
}
The class path concept and the logical difference between Java source code and compiled byte code is notoriously hard to get right.
I would strongly recommend you familiarize yourself with the Sun Java Tutorial. The relevant section is
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html

Categories