Daniel Oberlin
Daniel Oberlin

Reputation: 91

OSX / Eclipse / JDK / Can't run HelloWorld from bash

I'm trying to get back into Java development on OSX 10.9.5, and having a very basic problem getting a trivial program to run on the command line.

I've installed a recent JDK:

$ java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)**

I've installed Eclipse, version: Luna Service Release 2 (4.4.2)

I created a simple Java program within Eclipse, and I configured it to use the JRE version 1.8.0_45 that I installed.

I can run a simple HelloWorld program from this project within the IDE:

package repotools.repotool;

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

It shows the expected output within the Eclipse console.

I cannot get this to run from my bash shell. When I attempt to run it, I get the common error:

$ ls
RepoTool.class      RepoToolApp.class
$ java RepoToolApp
Error: Could not find or load main class RepoToolApp
$ java -cp ./ RepoToolApp
Error: Could not find or load main class RepoToolApp
$ export CLASSPATH=.
$ java RepoToolApp
Error: Could not find or load main class RepoToolApp
$

I don't have a .bashrc file, but it seems I can execute java and javac fine because they are in my path. There is no CLASSPATH set by default, but my understanding is that should not be a problem.

Any idea what I might be missing?

Thanks very much!

Upvotes: 1

Views: 61

Answers (1)

M A
M A

Reputation: 72874

The directory in which you execute java should be the one containing the root of the package containing the class. In this case:

$ cd ../..
$ java repotools.repotool.RepoToolApp

See http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/java.html:

By default, the first argument without an option is the name of the class to be called. A fully qualified class name should be used.

Upvotes: 3

Related Questions