0

I started to learn Java with lynda.com.

The one of the first lessons tells how to write simple Hello World program. This lesson introduses us with two console commands same in Windows, Linux and Mac:

javac <path_to_source>
java <name_of_class>

I do all similar to code implemented in lesson. Here is it:

package com.lynda.jt;
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Next you have to use these two commands to compile and run this program:

$ cd ~/HelloWorld
$ javac com/lynda/jt/HelloWorld.java
$ java com.lynda.jt.HelloWorld

But after running javac command it ends with error (I didn't save console output). Next I try to compile again but in other directory:

$ cd ~/HelloWorld/com/lynda/jt/
$ javac HelloWorld.java
$ cd ~/HelloWorld
$ java com.lynda.jt.HelloWorld
Error: Could not find or load main class com.lynda.lt.HelloWorld

Now its compiled and HelloWorld.class file appeared, but program still not running.

Next I delete .class file and do again:

$ cd ~/HelloWorld
$ javac com/lynda/jt/HelloWorld.java
$ java com.lynda.jt.HelloWorld
Hello World!

Now it's work but why? Previously I tried many other package diclarations:

com.lynda
com.lynda.javatraining
com.lynda.javatraining.hello_world
com.noq.jt

and others. But 99% of usesed paths lead to error messages. As to me it's just RANDOM.

Please clarify me this.

Noqrax
  • 1,049
  • 2
  • 8
  • 15

2 Answers2

2

I think you misrepresented at least one of your configurations/observations.

  • You need to make sure the class file is in the correct directory, based on its package.
  • You need to specify the name of the package and class properly on the command line (you had "lt" instead of "jt" at least once)
covener
  • 17,402
  • 2
  • 31
  • 45
0

It works either way. May be you would have navigated to the wrong directory before executing the java command.

Prudhvi Konda
  • 297
  • 1
  • 7