How to compile and run Java code with multiple packages or source files

10,811

Try the following

javac -classpath ./lib/*.jar ./src/*/*.java

Give a proper jar library path, if your java classes use a 3d party libraries. It's better to use a build tool with proper directory structure.

Share:
10,811
Tesla
Author by

Tesla

Updated on June 26, 2022

Comments

  • Tesla
    Tesla over 1 year

    I've been teaching myself Java, but I have only used Eclipse as a method of compiling and running my project, and would like to see how I would compile and run the following file structure using only the command line (Don't want to simply export a .jar file from eclipse).

    The file structure is as follows:

    src/
        model/
            file1.java
            file2.java
        app/
            main.java (contains main method)
            file3.java
            images/
                1.gif
                2.gif
        view/
            file4.java
            file5.java
    

    I need the image files compiled in as well, as I used them for a couple of things. I understand the javac command should be used to build the .class files, but I am confused as to who I would compile it (and run) with this file structure (and with the image files). Any help would be appreciated.

    Note: I am attempting to compile and run on Ubuntu 12.04 and am strictly trying to use command line.