MGoBlue93
MGoBlue93

Reputation: 684

Receiving errors when trying compile Java from command line

I'm trying to wean myself from IDEs -- getting to be lazy as a programmer. Before posting, I looked at these sites and it didn't help:

Error compiling Java from command line Compiling java from the command line Javac "cannot find symbol" http://www.javaprogrammingforums.com/java-ides/16906-trouble-running-java-file-windows-command-prompt.html#post71916

I have the following path to my source files:

D:\workspace\HelloWorld\src\com\dogzilla

There are two files in that path: Start.java...

package com.dogzilla;

public class Start{

    public Start() {

    }

    private static final String S = "Hello World";

    public static void main(String[] args) {

        HelloWorld hw = new HelloWorld();
        hw.printHelloWorld(S);
    }

}

...and HelloWorld.java

package com.dogzilla;

public class HelloWorld {

    public HelloWorld() {
    }

    public void printHelloWorld(String s){
        System.out.println(s);
    }

}

When I change the directory to D:\workspace\HelloWorld\com\dogzilla\src\main\java and run javac Start.java, it errors with:

Start.java:14: error: cannot find symbol
        HelloWorld hw = new HelloWorld();
        ^
  symbol:   class HelloWorld
  location: class Start
Start.java:14: error: cannot find symbol
        HelloWorld hw = new HelloWorld();
                            ^
  symbol:   class HelloWorld
  location: class Start
2 errors

So I've read you have to specify the classpath because of the package com.dogzilla; line.

So I change the directory to D:\workspace\HelloWorld and run javac -cp com\dogzilla\src\main\java\Start.java (interestingly enough, the tab key completes the path so I know the command prompt is finding Start.java just fine) it errors with:

javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options

How can I compile and run this otherwise simple program? The IDE hides this otherwise and I'm having problems learning it.

Upvotes: 0

Views: 2243

Answers (1)

tima
tima

Reputation: 1513

The first thing you have to do is to compile the java code into class files. The javac command is used for that.

You can run this command from anywhere, as long as you capture all of the java files that have to be compiled.

This will work:

[D:\workspace\HelloWorld\src]> javac com\dogzilla\*.java

this will also work:

[D:\workspace\HelloWorld\src\com\dogzilla] java Start.java HelloWorld.java

To run the compiled code, you use the java command. You have to use the fully qualified name of the main class so you have to be in the root source directory to do that.

[D:\workspace\HelloWorld\src]> java com.dogzilla.Start

Upvotes: 1

Related Questions