"Could not find or load main class [Class name]" - java

I pasted this code from a tutorial site and compiled the code in CMD as well as attempting to run the code:
public class ExampleProgram {
public static void main(String[ ] args) {
System.out.println("Hello World");
}
}
Since the PATH variable already existed, I added a path to the 'bin' file within the java file. I also added a new variable (CLASSPATH) and set the variable value to where I keep the .class file.
PS:
The .java and the .class file are in the same folder
No spelling or capitalization errors are made
I did not add .class at the end where I try to run the code

If you are working with "Netbeans" IDE and you want to run your program on command prompt(cmd),you should delete package statement or comment it by // marks. then use the following command:
javac yourProgName.java
java yourProgName
hope it be usefull

Assuming that you have you java path set up done coorectly, run the following commands in order :
JavaProg is the name of the java file and your public class.
1.Suppose my .java file is in Desktop, then run the following command
cd Desktop
2. Compile it
javac JavaProg.java
3. Run it
java JavaProg

Keep the .java file and remove the .class file.
Change to the directory of the .java file and compile the java file using the following command which will generate a .class file
javac ExampleProgram.java
Run the program by
java ExampleProgram

Related

classpath issues outside of Eclipse

I have a very simple piece of code I am trying to run on the Windows command line (Windows 7). It runs in Eclipse fine.
I have read How to make javac find JAR files? (Eclipse can see them)
and
https://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html#Understanding
but clearly am missing something or misunderstanding something.
Here's the code:
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.common.notify.Notifier;
public class MakeUniqIDs {
public static void main(String[] args) {
for (int i=0; i<10; i++){
System.out.println(EcoreUtil.generateUUID());
}
}
}
When I try to compile it with javac I get following error message: "MakeUniqIDs.java:1: error: package org.eclipse.emf.ecore.util does not exist"
I am in the src directory where the above code lives, and used the following to attempt to compile it:
javac -classpath "..\lib\org.eclipse.emf.ecore_2.13.0v28170609-0707.jar" MakeUniqIDs.java
I put the jar files in the lib directory, and also tried putting the path to the eclipse plugins directory into the classpath, but still no go.
You have to specify external JAR on the classpath.
java -cp path/some.jar; etc.
You are using classes that are part of Eclipse itself. You could dig out their JAR, but that's generally not a good idea. generateUUID() seems to be used to create a UUID in source file based on the file content, which is an IDE feature.
First ensure that the jar is in that path, and the name is exactly the same ls ..\lib or dir ..\lib. Then use the command (with the right path):
javac -classpath "..\lib\org.eclipse.emf.ecore_2.13.0v28170609-0707.jar" MakeUniqIDs.java
Also note that you have imported org.eclipse.emf.common.notify.Notifier;, and that class is in the jar org.eclipse.emf.common not ecore, you can remove the line (since you are not using it) or add the jar separated with ;.
Example:
javac -classpath "..\lib\org.eclipse.emf.ecore_2.13.0v28170609-0707.jar;..\lib\org.eclipse.emf.common_2.13.0v28170609-0707.jar" MakeUniqIDs.java

Cannot run this program from cmd with classpath option

this is a beginner question. I am having a problem running my java program from command line. I am using Windows10.
The problem is the following. I have a folder named "folder1", which is located o the dekstop of my computer. So the full path would be
C:\Users\Ioanna\Desktop\folder1
Inside that folder I have created a second folder which I named folder2. so the path to this would be
C:\Users\Ioanna\Desktop\folder1\folder2
Inside folder2 I have a java file named example.java
I want to compile it and run this file with setting the -classpath option through cmd. I dont want to set the path or to add the folder to tha path from environment variables.
I am trying
C:\Users\Ioanna\javac -cp C:\Users\Ioanna\Desktop\folder1\folder2 example.java
but it says file not found.
I tried several other alternatives, but I can't seem to find how to compile successfully the program.
Code compilation (to bytecode) and code execution are two separate steps, in Java.
First, compile your .java to obtain the corresponding .class file (I'm assuming your folder paths are right):
C:\Users\Ioanna\javac C:\Users\Ioanna\Desktop\folder1\folder2\example.java
This will give you example.class in that same folder.
Next, run that class (provided it has a main() method):
C:\Users\Ioanna\java -cp C:\Users\Ioanna\Desktop\folder1\folder2 example
java expects the path of the file(s) to compile. And example.java is not in the current folder (C:\Users\Ioanna).
Use
javac Desktop\folder1\folder2\example.java

Trouble running .Java file

I have a .java file known as "Warning.java" and its location is "C:\Users\chaos\Desktop\NEO-HACK" and I'm trying to run it using "C:\ProgramData\Oracle\Java\javapath\java.exe" but everytime I try to open the file I get an error message saying
"Error: Could not find or load main class
C:\Users\chaos\Desktop\NEO-HACK\Warning.java".
Here's the source code for my .Java file:
/*
This is the warning file
*/
public class Warning
{
public static void main(String args[])
{
System.out.print("NEO-HACK ACTIVATED");
}
}
You have to compile the code to a .class file before you run. You don't run .java files.
Please go through the Hello World tutorial thoroughly.
In order to compile a .java file to a .class file (Byte code), you need JDK(Java Development Kit) installed on your system. It seems you use Windows OS. Then, conventionally you will find this jdk folder at Program Files/Java/ (or Program Files(x86)/Java if you installed 32-bit JDK) if installed. In this JDK folder, you'll find a bin folder inside which you will have java compiler javac. If you want to compile your java code, you need to paste the java file in this javac containing folder i.e. bin and then from command prompt you will go to the path, compile using javac and run using java
>cd C:\Program Files\Java\jdk1.8.0_66\bin
C:\Program Files\Java\jdk1.8.0_66\bin>javac Warning.java
C:\Program Files\Java\jdk1.8.0_66\bin>java Warning
But, what if you want to compile and run java file that is placed at any location in your system? For this we need to set java bin path to Environment Variable.
Go to System Properties> Advanced tab and press 'Environment Variables' button.
Under System Variables, You will find Path variable. Select it and press edit button. Add an entry of your JDK's bin location. For me it is:
C:\Program Files\Java\jdk1.8.0_66\bin
This entry must be separated by semi-colon(;) from previous entries.
Press OK.
And now if you go to command prompt and type javac you will get all the options under javac. This makes sure that jdk's bin content are now accessible from anywhere in your PC.
C:\Users\chaos\Desktop\NEO-HACK>javac Warning.java
C:\Users\chaos\Desktop\NEO-HACK>java Warning
Hope this post simplified your Learning!!

couldn't find or load main class

I placed my Java code to the binfolder and try to run the code. The command javac Project.java terminated successfully, but the command java Project throws the error
couldn't find or load main class Project
This is my code:
Public class project {
public static void main(String args[]) {
}
}
You've got your error because of wrong syntax. Of course, java can't find Project class, there's no such thing declared. This is a correct declaration:
public class Project {
public static void main(String args[]) {
System.out.println(args[0]);
}
}
Note, that in Java class names start with upper-case letter, and access modifiers - with lower, like public, private, etc. I strongly suggest you to read Java Naming Conventions before writing any code.
If you're getting error like
couldn't find or load main class Project
there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions ), so you need to put in on the classpath with -cp option (as it mentioned by #Nikhil B). Note, that doing
javac -classpath "c:\java\jdk1.7.0.45"\bin" Project.java
which you posted in comments to his answer isn't correct. You should tell java interpreter where to find .class files, not java compiler (+ as I see, you've compiled your .java file just fine).
So, put the directory which contains .class file to a classpath somehow like this:
[root#crmdev clarify]# pwd //shows current directory
/home/clarify
[root#crmdev clarify]# javac Project.java //compiles .java file
[root#crmdev clarify]# ls Project.* //here are my test files for your case
Project.class Project.java
[root#crmdev clarify]# java -cp . Project "hello, #user5779261" //executing test code
hello, #user5779261
Run the java command with classpath option and it should run. (and change the class name to project from Project)
java -classpath "path to bin directory in double quotes" project

Again "wrong name" error when executing java program

With reference to this post
Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line
I did not understand how to solve the problem
Actually in my java source code there' s line :
package es_2011;
when I compile the program through JCreator everything works perfectly.
It creates a folder named es_2011 where to put .class files.
Also the executing operation goes smoothly, the program runs ok.
Now I'd like to use the command line only.
So I placed my java file in the directory where javac.exe is but whenever I try to compile I get the same error
The command I use is: javac ProgAudioJ.java
The path (where javac.exe is ) is : C:\Program files\Java\jdk1.6.0_22\bin
Is someone willing to help me understand in terms of exactly tell me what I have to do?
thanks very much...MAX
The setup used for the looks like this (under windows)
C:\classDir -> is the project
C:\classDir\testpackage -> is the only package used (the package "testpackage")
C:\classDir\testpackage\Main.class -> is the class with the main method inside (important: it is the .class and not .java)
The Main.class looks like following:
package testpackage;
public class Main {
public static void main(String[] args) {
System.out.println("Program started! ;-)");
}
}
go with your command prompt to:
c:\classDir> java testpackage.Main
the result:
Program started! ;-)
According to your problems that it starts in your IDE but not from the console:
- checked if you realy use the path to the .class files?
- with the console go to the directory of you .class files, not the project (e.g. in Eclipse it is the bin directory
- enter the full qualified class name (including packages seperated by . -> e.g. testpackage.Main
More infos can be found under:
http://www.oracle.com/technetwork/java/compile-136656.html
Hope it helped
MAX, if the class defines that it's inside the package es_2011, then it should be in a folder with the same name.
So in your case, put the ProgAudioJ.java in the folder es_2011 and then run
javac es_2011\ProgAudioJ.java
latter to run it, you need the command
java es_2011.ProgAudioJ
You should add javac.exe in your path .Edit your path variable and append path to jdk's bin
then put java file in a dir named es_2011 , as the package declaration is es_2011 then compile
c:\es_2011\javac YourJava.java
and now go back to C:
c:\java es_2001.Yourjava
After reading you other Post: "Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line" I guess you go to the directory es_2011 where your ProgAudioJ.class file is located and run
java ProgAudioJ
from that folder.
instaend you have to go to the folder above (cd ..) and run
java es_2011.ProgAudioJ
Each package in Java corresponds to a folder on the filesystem. So a package declaration such as com.stackoverflow would mean that the source classes need to be in a folder ./com/stackoverflow. Typically the whole project would have a separate src folder containing com/stackoverflow.
When you compile the Java classes you DO NOT need to put source files in the same directory as javac.exe, you do however need to make sure that javac.exe is in your operating systems PATH variable. This tells the operating system where it should look for executable files when a command is run, on a *nix machine this would usually be /usr/bin or just /bin but on Windows machine the executables normally live within the applications own directories, that is C:\Program Files\something. Assuming that you've installed JDK correctly, the javac.exe should already be in the PATH you can check this by opening the command line and just running javac (just like that). If you get some output then all is well, the system knows where to find javac.exe.
Next you will need to go to your project folder and type javac -d . src/com/stackoverflow/MainSO.java notice that is run from the project folder. This will create a folder called com in your project root and put the compiled classes in com/stackoverflow. The -d flag tells javac where to put the compiled classes, if you leave that out, the compiled classes will be where the sources are.
Then when you want to run the classes you type java com.stackoverflow.MainSO (no .class). Crucially this command will need to be ran in the directory that contains the root of the class hierarchy (that is the com folder containing the compiled classes). You can specify other places for java to look for the classes by providing a classpath to the java command with the -cp flag. By default the classpath will contain the directory the java command was ran in. Should your project have dependencies external .jar files for example you will need to provide every single one of them (with their full filepath) in the classpath (this goes for the compiler as well). The IDEs do this automatically for you.
Hope that helps.

Categories