couldn't find or load main class - java

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

Related

How to compile and run java with package

HI I'm having trouble with my java compille
I made folder named 'Test'. In this folder i make two folders, one is src, another one is bin. Then I made Test. java in that src folder
package Test;
import java.io.*;
public class Test {
public static void main(String args[]) {
System.out.println("hi");
}
}
i saved it and back at Test folder and then i compile like this
javac -d bin src/Test.java
Thus, i have Test folder in bin folder.
finally in Test folder i write this command
java -cp bin/Test Test
unfortunately, it says can't find Test class
How can i run this code???
When your class is in a package, the name of the class includes the package. Thus Test.Test is the Test class in the Test package. -cp bin tells java that the classpath starts in bin.
java -cp bin Test.Test
# classpath main-class
"-cp" expects a directory, not a file. Give it the ./bin/ directory, not the file you're trying to execute.
java -cp bin Test.Test

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

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

When should I set class path?

public class a {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
For the above code, I can run it by javac a.java, and then java a.
But if I add a package for it:
package hello;
public class a {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
I need add the classpath -cp in order to run it: java -cp ../ hello.a
Why I do not need to set the classpath in the first situation? When do I need to add -cp?
To answer your question
When should I set my classpath
Always, as you work on more complex projects you will find that your classpath will almost always need to be set. This will either be done manually like you have done with -cp command or by your IDE.
To answer your second question
Why I do not need to set the classpath in the first situation
I first need to explain a little bit about classpaths. In short classpath exist to tell the VM where to look for your files. In the first situation since you didnt have a package the default location was used to find your class so no classpath was needed. However when you complicated things and added a package at this point a classpath is needed
The classpath is where java (the program) looks for classes. The default contains a bunch of system-wide things (for the JDK), and then also the current directory: ..
Without the package line, your class was in the "default package," which is basically no package. This means its full name is a (more or less), and java will look for it in a file called $CP_ELEM/a.class for each element CP_ELEM in the classpath. In the default case, that amounts to ./a.class, which is fine because that file exists.
With the package line, your class is in the hello package, and its full name is hello.a. That means that java will look for it in $CP_ELEM/hello/a.class, which amounts to ./hello/a.class -- which doesn't exist. But if the directory you're in happens to be called "hello", then java -cp .. hello.a, which amounts to looking in ../hello/a.class, will work.
Classpath is like telling the system where to find my classes:
If you don't have a classpath the java will try to load the class
from the default directory (Probably where you're running the command at).
Now let's say you put your compiled classes in folder "bin" and sources in "src" folder
To tell the system to load the classes from "bin" folder you have to give him the following parameter:
-cp bin;
Also i see you don't understand the packaging system in java so here's fast explain:
Packaging is like you the directory of your class for example:
If you set the class's package to package a; and your classpath directory is set to "bin"
You have to create folder called "a" in "bin" folder, and then move the compiled class there, do the same for the source file, but in "src" folder
Just saying you could use eclipse which is located at : http://www.eclipse.org
If this didn't help you, Then take a look at this: https://www3.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html

Error java executing: Couldn't find or load the main class

I'm trying to execute a simple java code (I have already compiled it with no problems) but it gives me the next error:
c:\Users\alejandro\Desktop> java HelloWorld.java
Error: Couldn't find or load the main class.
The code is the next:
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello world!");
}
}
I have set the PATH variable correctly.
I have deleted CLASSPATH variable.
I have both files (.java and .class) in my Desktop.
You're specifying the name of the source file. That's not what you provide to the java command - you specify the class name.
java HelloWorld
This assumes that HelloWorld.class is somewhere on the classpath, which would default to "the current directory".
If you had a package, e.g.
package foo;
public class HelloWorld {
...
}
Then you would want to put HelloWorld.java in a directory called foo, and compile and run from the root directory:
> javac foo\HelloWorld.java
> java foo.HelloWorld
Note how now the fully-qualified class name is foo.HelloWorld, not foo\HelloWorld.
when you run the compiled file, you should use only the class name. The compiled file will have an extension of .class but you should not add any extension. Just use the class name.
change
c:\Users\alejandro\Desktop> java HelloWorld.java
to
c:\Users\alejandro\Desktop> java HelloWorld

package in .java file makes class file unuseable

It's been too long since I've last done Java, and I can't remember why the following happens:
Given this file, created by a standard Maven project, as can be seen here: Maven Tutorial
package com.mycompany.app;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
Compiling this, not even with Maven but with the standard javac executable, will generate a class file without errors. Try to run the class file, and you get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: App (wrong name: com/mycompany/app/App)
Remove the package command, compile again and it runs just fine. Why is this? I'm running JDK 1.6.0_21 btw.
One thing you must do after creating a package for the class is to create nested subdirectories to represent package hierachy of the class. In your case the package name is "com.mycompany.app" so the App.class (compiled App.java file) should reside in "com/mycompany/app" sub-directory. It doesn't matter where the source file is residing though. For example, I have copied your file and did the following:
$ ls
App.java
$ javac App.java
$ ls
App.class App.java
$ mkdir -p com/mycompany/app
$ mv App.class com/mycompany/app/
$ java com.mycompany.app.App
Hello World!
$
Please read Wikipedia page about Java Packages for more information. You can also take a look at these links:
The Java packages tutorial
Java packages tutorial
Oracle's notes on packages
Good luck!
When you attempt to execute your program, it will look for the class file using the path specified in the package. So, when you have the package statement in the file, your class file must be in the com/mycompany/app/ directory (relative to what directory you're attempting to run it from); if it can't find it, you get that exception.
Thus, when you remove that package statement, the JVM will look for it in current directory, which is why it works (because you're executing java App in the same directory in which the App.java and App.class files exist).
You need to add the com/mycompany/app folder to your Java CLASSPATH . If I remember well, you can also do it from the cmdline using the parameter "-cp".
This is because in Java filesystem files map to classes (e.g. each public class must be in a separate eponymous file) and packages map to directories.
So if you have a class which is in the com.mycompany.app package it must be in com/mycompany/app directory relative to the classpath.
In your case you should have an output directory, say and the you should have the class in /com/mycompany/app/App.java. Then you build it, running javac from and giving com/mycompany/app/App.java as parameter, instead of com/mycompany/app/App.java.
Running the class works in an analogical way, but you give the fully-qualified-name of the class, instead of the directory path.

Categories