-3

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[]) {
    }
} 
slartidan
  • 20,403
  • 15
  • 83
  • 131

2 Answers2

2

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
solar
  • 1,344
  • 1
  • 8
  • 14
1

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

Nikhil B
  • 76
  • 3