14

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 answer but nothing has worked.

So I have

src/
  Support/ (.java files)
  Me/ (.java files) 
  Wrapers/ (.java files)  

Do you know how to compile everything with javac?

John Topley
  • 113,588
  • 46
  • 195
  • 237
Altober
  • 956
  • 2
  • 14
  • 28

7 Answers7

12

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.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 2
    +1 - For an existing project with minimal external dependies, Ant is simpler than (for example) Maven. His IDE may even be able to generate an initial "build.xml" file. – Stephen C Aug 18 '10 at 13:39
  • Thanks I tried this, it compiles everything but it does not generate the class structure so when I try to run my main it says ""main" java.lang.NoClassDefFoundError: Wrapper/java" – Altober Aug 18 '10 at 13:46
  • 1
    @Altober: the `java` tool expects *class names*, not file names, and especially not source file names. You probably need to do something like `java Wrapers.Wrapper`, assuming you have a class "Wrapper" inside a package "Wrapers" – Michael Borgwardt Aug 18 '10 at 13:50
10

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

pavanlimo
  • 4,122
  • 3
  • 32
  • 47
  • 1
    yeah the problem is that I need to do it in an external server and I am not able to install any aditional too. – Altober Aug 18 '10 at 13:39
  • You can unzip ant / maven in the same file system you store your code, you don't need to "install" them, so this is not going to prevent you from using either – Jon Freedman Aug 18 '10 at 13:43
6
javac -d compiled $(find src -name *.java)
Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
2

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

pratyush
  • 21
  • 2
2

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
Moritz
  • 14,144
  • 2
  • 56
  • 55
1
  1. To compile Run below command [it will store all class files in classes folder]

    javac -d classes Support/*.java Me/*.java Wrapers/*.java

**Note : classes folder should be created first

  1. To Run java application, run below command

    java -cp classes {mainfile_name}

Replace mainfile_name with your main file.

chetan mahajan
  • 723
  • 7
  • 9
0

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
aepryus
  • 4,715
  • 5
  • 28
  • 41
  • What if you refactor a package or add a new package, you have to touch this file every time? Have a watch of http://skillsmatter.com/podcast/java-jee/zen-and-the-art-of-build-script-maintenance if you need convincing... – Jon Freedman Aug 18 '10 at 13:56
  • 2
    No idea about others. But over the course of a project the amount of times I need to modify such a batch file is miniscule, if at all. There are no doubt situations that Maven and Ant is warranted; but I have not encountered them. Sometimes to kill a fly, a shutgun is not necessary (nor a nuke) – aepryus Aug 18 '10 at 14:10