views:

97

answers:

6

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?

+5  A: 

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
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
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
A: 

javac is a compiler. What you need is a build system, which will be responsible for calling javac for multiple source files/paths.

One of the most popular tools doing that is Ant.

Samuel_xL
In fact `javac` is perfectly capable of doing simple structures like that. Other compilers need their hands held for every kind of dependency resolution, but `javac` is smart enough for it.
Joachim Sauer
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
@Altober: It's been explained elsewhere, but you can just copy ant somewhere and it'll work; the installation process for ant is just "copy this over there". That said, for this, ant is overkill.
Dean J
+4  A: 

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
+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
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
@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
@Michael YOU ARE A GENIUS...IT WORKED FINE. Thank you all of you guys.
Altober
+1  A: 
javac -d compiled $(find src -name *.java)
Ken Bloom
thanks I tried but I am getting "illegal variable names" do you knoe why. Thanks
Altober
maybe you need `-sourcepath src` as well. (or you need to specify the `src` directory as part of the classpath)
Ken Bloom
+1  A: 

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
+1  A: 

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
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
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