2

I understand the command would be javac file_name.java but how would I put together a shell script which could compile several java files?

I was also thinking about copying the files, which I presume I just use cp and absolute file path referencing.

2
  • 5
    The usual way is to use a build tool like make or ant. Commented Aug 18, 2012 at 21:16
  • Why do not you use maven? (maven.apache.org) Commented Aug 18, 2012 at 21:41

2 Answers 2

1

Create a .sh file and add the following contents. Make the file as executable and run it. (Specify the complete path along with the file name)

#! /bin/sh
javac sample.java
Sign up to request clarification or add additional context in comments.

4 Comments

This will recompile every script each time called, which is less than efficient.
If I wanted to copy my java file into the location where javac is located, how do I make sure each time I copy my java file it overwrites the previous version? Is there an argument for the cp command?
cp will overwrite if file exists. And Why do you want to copy the file to your javac location. just add the java/bin to your environment and you can use javac anywhere.
@AkhilDev I am a bit of a newbie to Unix, can you please explain how I do this? Thanks
0

Try this script: compile_java_files.sh

#!/bin/sh

typeset -r JAVA_FILES_DIR=$(cd full_path_to_java_files 2>/dev/null ; pwd)   # JAVA FILES DIRECTORY

LOG_DIR="/tmp/java_compilation/logs"    # Create this dir or use another one

for java_file in `ls $JAVA_FILES_DIR`;
do
    javac $java_file
    return_status=`echo $?`
    if [ $return_status -ne 0 ]
    then
        echo "Failed to compile $java_file" >> $LOG_DIR/$java_file.ERR

        exit 1
    fi
done

Then run your script(don't forget to specify the path to the directory that contain java files):

chmod +x compile_java_files.sh
./compile_java_files.sh

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.