3

I am compiling my project written using eclipse using the command line as follows

javac file.java

And then to run:

java file (args here)

How would I run a clean build, or compilation? Whenever I recompile, the changes are not getting affected unless I delete all the .class files and recompile. Please note that the the main file will call other classes too.

Thanks

2
  • 1
    Is there a reason you're compiling by hand at the command line if you've got your project in eclipse already?? Commented Sep 7, 2011 at 20:09
  • Eclipse isn't taking the file name in the argument. It keeps saying file not found. Commented Sep 7, 2011 at 20:11

2 Answers 2

4

You should compile all the Java files in your application to be sure, basically.

Options include:

  • Use a full build system such as Ant.
  • On a Unix box, use something like:

    javac `find . -name '*.java'`
    

    assuming you don't have too many (or use xargs if necessary).

  • If you're not using packages (and you really should be) you can just use:

    javac *.java
    
Sign up to request clarification or add additional context in comments.

3 Comments

Oh yeah - that is the problem.. I am running Mac OS X. I used your code above and it didn't work. How do I compile all .java files in a directory?
Also, it is a good idea to write all your output to a target directory, using the -d command line option. Deleting old content is then as simple as deleting all content in the target directory.
If OP isn't using packages, and all the .java files are in the same directory, just use javac *.java. It's simpler, and it has the advantage of working on platforms that don't have back-tick substitution and/or the find command.
2

Here's how I handle this.

rm $(find . -name "*.class")
javac <MainClass>.java
java <MainClass>

It's a simple trick that removes all files ending with the extension ".class". When you compile with javac, it will compile all the files again without you having to worry about directory structure either.

In case you are on windows you can get the alternative for find here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.