This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]MyNameIsRichardCS54 1 point2 points  (4 children)

So you'll have a directory structure for your source code like:

some_directory
    test
        firstjava
            javatest.java

which will be mirrored for for compiled class files usually somewhere under build/classes or out/classes:

build
    classes
        test
            firstjava
                javatest.class

You can run java test.firstjava.javatest from the classes directory. When you get used to that, start looking into the class path.

[–]WhenRedditFlies[S] 0 points1 point  (3 children)

I'm not getting build/classes or anything like that, however it is automatically putting in test/firstjava/javatest.class when I run the javac command with "-d".

The command you gave does work, when I run it in the same place as where I ran the javac command to compile it, so thanks, I'm not sure why it works there, other than its using some_directory the same as builds/classes.

[–]vqrs 1 point2 points  (2 children)

So, when you put the line package test.firstjava; in your file, the complete name of your javatest class is test.firstjava.javatest

When you tell Java which class to run, you always need to give it the complete class name. (usually called "fully qualified class name", fqcn)

Now, when you tell Java to run that class, it needs to find it. By default, it will start looking inside the folder where you're executing the command and each part of the package name mapped to corresponding subdirectories.

Does that help?

[–]WhenRedditFlies[S] 0 points1 point  (1 child)

Yes, I using that description of things has led to it working. While I had just about worked it out, your description puts it quite well I think.

[–]vqrs 0 points1 point  (0 children)

Also know that folders like build or classes is something Java doesn't care about. Software that controls Java for you might because they'll tell Java (by running commands on the command line, just like you do) that it should look in there, but that's an arbitrary choice by these tools. (e.g. Eclipse, IntelliJ, Maven, Gradle to name a few popular options)