Compiling multiple packages using the command line in Java

后端 未结 6 1569
余生分开走
余生分开走 2020-11-30 08:05

Hi i have been using an IDE but now I need to run and compile from the command line.

The problem is that I have multiple packages and I have tried to find the answe

相关标签:
6条回答
  • 2020-11-30 08:33

    If you really need to just use javac and standard UNIX commands you could to this:

    find src -name \*.java -print0 | xargs -0 javac -d classes
    
    0 讨论(0)
  • 2020-11-30 08:34

    This should do it (may require additional classpath elements via the -cp command line switch):

    javac Support/*.java Me/*.java Wrapers/*.java
    

    But if your build process gets more complex (and it will!), you should look into using Apache Ant for build automation.

    0 讨论(0)
  • 2020-11-30 08:39

    The real answer is javac -d (places where classes to be built and placed) -sourcepath (source of the package at the root) -cp (classpath of the dependencies which can again be classes folder where the classes are build and kept) full qualified name of the java file.

    Ex javac -d classes -sourcepath src -cp classes src\com\test\FirstSample.java

    The FirstSample.java contains the main method. Pacjage structure mentioned below.

    Before compiling
    HomeApp
    --src
    ------com\test\FirstSample.java (First Sample using the FirstPojo.java)
    ------com\test\FirstPojo.java
    --classes

    After compiling
    HomeApp
    --src
    ------com\test\FirstSample.java (FirstSample.java using the FirstPojo.java)
    ------com\test\FirstPojo.java
    --classes
    ------com\test\FirstSample.class (FirstSample.class using the FirstPojo.class)
    ------com\test\FirstPojo.class

    0 讨论(0)
  • 2020-11-30 08:40
    javac -d compiled $(find src -name *.java)
    
    0 讨论(0)
  • 2020-11-30 08:42

    In many cases Ant is overkill. Just use a BAT file if you are in windows or a shell script (sh file) if you are in linux. You can create a text file which includes all your javac commands and just run that file when you want to build.

    For example, I use the following bat file to build one of my apps:

    @echo off
    echo Building Shazaam...
    
    del classes\com\aepryus\shazaam\*.* /q
    del classes\com\aepryus\shazaam\engine\*.* /q
    del classes\com\aepryus\shazaam\domain\*.* /q
    del classes\com\aepryus\shazaam\persist\*.* /q
    del classes\com\aepryus\shazaam\view\*.* /q
    del classes\com\aepryus\shazaam\task\*.* /q
    del classes\com\aepryus\shazaam\action\*.* /q
    del classes\com\aepryus\shazaam\controller\*.* /q
    
    javac src\com\aepryus\shazaam\*.java        -classpath \lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepLoom.jar; -d classes
    javac src\com\aepryus\shazaam\engine\*.java -classpath \lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepLoom.jar;\lib\Sprout.jar;classes; -d classes
    javac src\com\aepryus\shazaam\domain\*.java -classpath \lib\AepUtil.jar;\lib\AepLoom.jar;\lib\Sprout.jar;classes; -d classes
    javac src\com\aepryus\shazaam\persist\*.java    -classpath \lib\AepUtil.jar;\lib\AepLoom.jar;\lib\Sprout.jar;classes; -d classes
    javac src\com\aepryus\shazaam\view\*.java   -classpath \lib\Servlet.jar;\lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
    javac src\com\aepryus\shazaam\task\*.java   -classpath \lib\AepUtil.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
    javac src\com\aepryus\shazaam\action\*.java -classpath \lib\Servlet.jar;\lib\AepUtil.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
    javac src\com\aepryus\shazaam\controller\*.java -classpath \lib\Servlet.jar;\lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepRPC.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
    
    cd classes
    jar cf ..\war\WEB-INF\lib\Shazaam.jar .
    cd..
    
    echo Complete
    
    0 讨论(0)
  • 2020-11-30 08:43

    You should use build tools like Maven or Ant for such tasks.

    In the initial stages, when the project is not very complex you can use the following line to compile, with appropriate classpath in place(as suggested by @Michael):

    javac Support/*.java Me/*.java Wrapers/*.java

    0 讨论(0)
提交回复
热议问题