Search code examples
javacommandjavac

Java command to run the main class


I have been away from Java stuff for quite sometime, need some help about basic stuff.

I have the following project structure :

enter image description here

There are no manifest files etc, its just a raw folder structure.

I wanted to know command to compile these java classes from root folder Data Structures - Java and command to execute compiled classes.

I did try

javac -d build com.codesuman.datastructures.Main

This worked first time but failed in next attempt.

Thanks in advance.


Solution

  • I don't see how that could work, even a single time :

    javac -d build com.codesuman.datastructures.Main
    

    In javac, the last argument is "source files". It has to specify location of java source to compile in terms of filesystem location : that is file or directory.
    But that com.codesuman.datastructures refers to a java package. Something like that is expected : com/codesuman/datastructures/Main.java.

    So, to compile that class in the build directory, do that :

    javac -d build com/codesuman/datastructures/Main.java
    

    But if Main.java relies on other classes, which looks possible according to your snapshot, you also need to compile these classes.
    So a more idiomatic approach in this case is :

    javac -d build com/codesuman/datastructures/*.java
    

    But beware the subfolders are not compiled.