-1

I looked around, but none of the solutions that worked for others worked for me.

I installed java 1.8.0

My path variable is C:\Program Files\Java\jdk1.8.0_05\bin

I try to run the following program hello.java:

package hello;

public class hello{
    public static void main(String[] args){
      System.out.println("Hello");
    }
}

The program compiles fine when I run javac hello.java

But when I use java hello , java -cp . hello, or java -classpath . hello it returns the error 'Could not find main class hello'.

I know this is a very basic problem, but I really can't figure it out.

Thanks in advance

  • 7
    You should use the complete name of the class, which is `hello.hello`. – Luiggi Mendoza Aug 27 '14 at 22:20
  • 1
    For a better [naming convention](http://www.oracle.com/technetwork/java/codeconventions-135099.html), the first letter of your class should be capitalized, say `Hello`. – Wenbing Li Aug 27 '14 at 22:51

4 Answers4

4

In very similar answers that I've provided, provided that you're compiling in the current directory, then you need to ensure that your compiled class has made its way to a folder called hello/.

If it has, then you can run this:

java -cp /path/to/hello hello.hello

The above adds the hello/ folder to the classpath, and then you can run the main class using its fully qualified name.

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

Here you define package so you cannot run directly your compiled class because JVM is not able to find your class so you have to write the path of your hello directory in run command. for example:

java -cp c/workspace/hello hello

Pushpendra
  • 2,791
  • 4
  • 26
  • 49
0

I try your code. And see what the problem is. If you put the "hello.java" in a folder named "JavaTrials" and compiled it there as "javac hello.java", it compiles and produces "hello.class" right there. This command does not produce a folder named "hello" for the package.

Your compiled code "hello.class" should be in a folder named "hello" which is the name of the package. And then you have to run the command "java hello.hello" not from inside the folder "hello" but from the containing folder.

The better way is to put your code in a folder named "hello" before compiling it. This folder represents the package. Then compile it from the outside of the "hello" folder with command "javac hello/hello.java". And then you can run it by "java hello.hello"

As a side note, in java coding tradition class names begins with upper case. Se better use "Hello" instead of "hello".

Nuri Tasdemir
  • 9,720
  • 3
  • 42
  • 67
-5

remove first line "package hello;"

Justin
  • 1,356
  • 2
  • 9
  • 16
  • 5
    While this is an option, it won't help OP to learn how Java works. Also, working with default package is a bad practice. – Luiggi Mendoza Aug 27 '14 at 22:24
  • It worked. Thanks! Don't know why I even added that line. Some internet turtorial thaught me to always include it. – user3527702 Aug 27 '14 at 22:31
  • 1
    Since this is obviously his first java Hello World program, it's probably ok to leave out the package. – Justin Aug 27 '14 at 23:08
  • 1
    Handling packages and classpaths is an _intrinsic part_ of using Java, and so I'd argue it not okay to leave it out – jdphenix Aug 27 '14 at 23:13
  • I am just saying for someone's absolute first Java program it is probably ok to leave it out since packages and classpaths are probably not going to be covered in your absolute first java program. Just look at every single java Hello World program example: https://www.google.com/#q=hello+world+java+program&revid=461964232, no packages to be found. I'm not saying it's not important, but you cannot learn everything on your first day, even if it is an intrinsic part of using Java. – Justin Aug 27 '14 at 23:29
  • Since it is the first absolute Java program, it should be done **in the right way**. – Luiggi Mendoza Aug 28 '14 at 02:24
  • I think you are right when you say in a "Hello World" program there should be no package and it should be removed. However just saying "remove first line" kind of implies this is the source of the problem, a mistake, but it is not. – Nuri Tasdemir Aug 28 '14 at 02:51
  • Just because it's a common find when you Google search "hello world java" doesn't make it a good idea. – jdphenix Aug 28 '14 at 02:53