0

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.

MGoBlue93
  • 644
  • 2
  • 14
  • 31
  • 1
    your missing the `import HelloWorld` in `Start` class ? – hoan Apr 16 '18 at 03:49
  • @tima... no it's not a duplicate. – MGoBlue93 Apr 16 '18 at 04:20
  • @tonyhoan... why would I need to import? HelloWorld? I don't need to in an IDE... HW is in the same classpath and in the same package afterall. – MGoBlue93 Apr 16 '18 at 04:21
  • @MGoBlue93 you have tried `javac Start.java HelloWorld.java`? – tima Apr 16 '18 at 04:21
  • @tima... Yes, javac Start.java HelloWorld.java works. I believe the problem, based upon previous reading, is due to my package statement. The IDE handles that. The CLI does not -- the user has to specify the parameters. I'm clearly just not specifying the right thing. When I try to run java Start, I get Error: Could not find or load main class Start. When I try java -cp com.dogzilla Start or java -cp com.dogzilla.Start it fails as well. – MGoBlue93 Apr 16 '18 at 04:28

1 Answers1

1

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
tima
  • 1,498
  • 4
  • 20
  • 28
  • Thanks! That worked. I had read previously that you need to do java -cp com.dogzilla Start. Using java com.dogzilla.Start works instead! – MGoBlue93 Apr 16 '18 at 22:07
  • @MGoBlue93 you can use `-cp` this way: `java -cp . com.dogzilla.Start`. Notice the dot which points to the current directory (src directory). `-cp` is used to specify the location of additional jars that are needed for your application, but you are not using any other jars so its not needed for now – tima Apr 16 '18 at 22:35