Ubuntu script to compile multiple java files? - java

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.

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

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

Related

can't make a shell script to make a jar file

I try to make a simple shell script to make a jar file. The jar command combined with -C does not work with wildcards. Therefor I use a wildcard to find the files I want. Write them to a file, and loop over them.
It looks something like this:
the_classes=''
cd "$bin_folder"
tmp_dir=$(mktemp -d -t java_sucks)
find "imui/core/" -type f -name "IMUI_Widget_Agent*.class" >"$tmp_dir/classes.txt"
while IFS="" read -r p || [ -n "$p" ]
do
the_classes="${the_classes} -C '$bin_folder' '$p'"
done < "$tmp_dir/classes.txt"
Using the above I complete the command:
cmd='jar cfm build/IMUI_Widget_Agent.jar'
cmd="${cmd} \"$bin_folder/imui/core/IMUI_Widget_Agent_MANIFEST.MF\" $the_classes"
printf "\n\n\ncmd\n\n\n"
echo $cmd
Now if I copy and paste this command to execute it works!
But I want to avoid the manual labour of doing the copy and paste by hand every time.
Now I have:
eval "$("$cmd")"
But I get an error File name too long. No matter what I try, every fix I do creates a new problem. I have been working 6 hours now to make this script.
What would be a good step forward?
Since you cd "$bin_folder" you don't actually need -C "$bin_folder":
#!/bin/bash
shopt -s globstar
cd "$bin_folder"
jar cfm build/IMUI_Widget_Agent.jar \
imui/core/IMUI_Widget_Agent_MANIFEST.MF \
imui/core/**/IMUI_Widget_Agent*.class
However, if you still want to add them as part of a larger script, you can easily and robustly build your command in an array:
#!/bin/bash
shopt -s globstar
cmd=(jar cfm build/IMUI_Widget_Agent.jar imui/core/IMUI_Widget_Agent_MANIFEST.MF)
cd "$bin_folder"
for file in imui/core/**/IMUI_Widget_Agent*.class
do
cmd+=(-C "$bin_folder" "$file")
done
echo "About to execute: "
printf "%q " "${cmd[#]}"
echo
"${cmd[#]}"
Alternatively, you can simply do eval "$cmd" with your code, which is equivalent to echo and copy-pasting. However, be aware that this is fragile and error prone because it requires careful escaping of the filenames which you're not currently doing.

Shell script with jar file at the end

I download an archive file. In the archive there will be a file that has a .sh. extension. When I opened that file with VI I found the below code in the beginning of the file:
#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
java=java
if test -n "$JAVA_HOME"; then
java="$JAVA_HOME/bin/java"
fi
exec "$java" $java_args -jar $MYSELF "$#"
exit 1
I can run the jar by doing java -jar file or `./file'.
Can someone explain me what is going on? How can you create such file?
Try by yourself the following commands. Start creating a normal jar file with any content, or use someone you have. I will name it "myjar.jar"
Next, create a file "hello.sh" with content:
#!/bin/bash
exec echo hello
now, add this file at start of a new jar file:
cat hello.sh myjar.jar > mytrick.jar
chmod 700 mytrick.jar
And finally, the interesting part, type:
./mytrick.jar
jar -tf mytrick.jar
unzip mytrick.jar
in other words, usually jar/unzip skips any content until their own header. Moreover, a shell script "ends" in a line who call "exec" (because shell interpreter is replace at this point by the command in the exec line).
However, this trick is based in a behaviour of jar/unzip probably out of standards. Note, by example, that this statement fails (has no effects):
jar -xf mytrick.jar
If the file after extracting the tar file is start-superbeam.sh try doing chmod +x start-superbeam.sh && ./start-superbeam.sh or /bin/sh ./start-superbeam.sh.
If the program has arguments, supply them after at the end. It will run java on that the superbeam.sh which as a jar file at the end.
If you need special java parameters set such as for memory size, you would set them in the environment variable java_args.
As for what's going on this is a shell script with a jar file at the end of it after the exit. To quote from ReallyExecutable Jars:
There has long been a hack known in some circles, but not widely
known, to make jars really executable, in the chmod +x sense. The hack
takes advantage of the fact that jar files are zip files, and zip
files allow arbitrary cruft to be prepended to the zip file itself
(this is how self-extracting zip files work).
As for how to create, see the accepted answer or the link.

how to create a jar with compiled classes from a source jar in java

I know how to create a jar from .java or .class files.
I have a sources.jar that contains .java files. I want to compile these classes and pack into a jar again.
for example:
This is the jar I want to compile:
hadoop-mapreduce-examples-2.2.0-test-sources.jar.
I did jar xvf hadoop-mapreduce-examples-2.2.0-test-sources.jar to extract everything.
I now have
ls
META-INF org
ls org/apache/hadoop/
examples mapreduce
ls org/apache/hadoop/examples/
TestBaileyBorweinPlouffe.java TestWordStats.java pi terasort
As you can notice, I have two packages within org.apache.hadoop, "examples and mapreduce", both of which have to be compiled. And I have sub-packages within "examples" that needs to be compiled.
javac org/apache/hadoop/mapreduce/lib/db/TestDBJob.java
This compiled fine
But How can I compile recursively. I tried using wildcard '*'.
This is what I tried:
javac -cp org/apache/hadoop/examples/*
javac: invalid flag: org/apache/hadoop/examples/pi
Usage: javac <options> <source files>
use -help for a list of possible options
javac org/apache/hadoop/examples/*.*
In the last javac executed above, I don't see the subpackages being compiled. only the top-level java files was compiled.
Is there a simple solution such as
javac -jar <input.jar> <output.jar> will result in <output-jar> that has compiled files in it.
EDIT:
To re-iterate my question:
given a jar that has only .java files, I want a jar file that has .class files in the simplest possible way
From the DOS console
cd /dir/folder of .class files.
jar cvf name.jar
Here's a really dirty script I wrote to build compiled JAR out of a sources JAR. It probably won't work for everything, and doesn't even handle copying the Manifest:
#!/bin/bash
if [ "$#" -lt 1 ]; then
echo "Usage: $0 sources.jar [classpath]"
echo
exit 1
fi
OUT_JAR=`basename $1-compiled.jar`
rm -rf $OUT_JAR
rm -rf src
rm -rf classes
mkdir src
mkdir classes
if [ -n "$2" ]; then
CLASSPATH="-cp $2"
fi
unzip -d src $1
if [ $? -ne 0 ]; then
echo "Unzip failed"
exit 1
fi
find src -name *.java | xargs javac -d classes $CLASSPATH
if [ $? -ne 0 ]; then
echo "Java compilation failed"
exit 1
fi
jar cf $OUT_JAR -C classes .
if [ $? -ne 0 ]; then
echo "Jar creation failed"
exit 1
fi
echo
echo "Compiled and built $OUT_JAR"
Also available here: https://github.com/satur9nine/sources-jar-compiler/blob/master/compile-sources-jar.sh

how to execute jar file in hadoop platform in ubuntu?

I can create Mapreduce programme, so i can configure hadoop in eclipse. after i can create 1. mapper, 2. reducer, 3.mapreducerDriver
after i can create jar file with help of Makefile in shell command prompt,
after i can use
this command
hadoop jar $ {JarFile} $ {MainFunc} input output
make file
JarFile = "Sample-0.1.jar"
MainFunc = "mypack.Mapreduce"
LocalOutDir = "/ tmp / output"
after i use
jar-cvf $ {Sample-0.1.jar}-C bin /.
jar file created , finally i can write this command.
hadoop jar $ {Sample-0.1.jar} $ {mypack.Mapreduce} input output
finally i get like this error will come in command prompt.
bash: ${mypack.Mapreduce}: bad substitution
how can i solve this problem . pleasae help me
now i find sollution
hadoop jar $ {Sample-0.1.jar} mypack.Mapreduce input output
then hadoop will be run .
Write a script like compile.sh
$ mkdir wordcount_classes
$ javac -classpath ${HADOOP_HOME}/hadoop-${HADOOP_VERSION}-core.jar -d wordcount_classes WordCount.java
$ jar -cvf /usr/joe/wordcount.jar -C wordcount_classes/ .
For reference: http://hadoop.apache.org/docs/r1.0.4/mapred_tutorial.html

Using Bash to automate creation of test outputs

I have a java program that I am trying to generate 3 outputs for, and then rename them depending on what the input file was originally called.
The problem is that only the input file is being moved. I think this might be an issue regarding relative file commands.
Here is my script. (Also, I'm open to suggestions on making this script better looking. I'm a bash newbie.)
#!/bin/bash
########################################################
#This script compiles Main, then attempts to run each #
#test case. After running a test case, it renames the #
#testcase and moves it to a new directory. #
########################################################
#echo `pwd` <---- This was used for testing. pwd is correct
#Gets contents of "tests" directory, stores them into the array
#without the file extension.
list=(`ls tests| sed 's/\.txt$//g'`)
#Compiles Main.java
cd ./src
javac Main.java
cd '../'
mv -f src/*.class bin #*/ (formatting fix)
#Runs Main for each test case, then renames and moves the test cases.
for filename in ${list[#]}
do
echo 1 > input.txt
echo tests/$filename.txt >> input.txt
cd ./bin # Why do I need to cd to make this work?
java bin/Main < input.txt
cd ../
mv -f input.txt "scriptout/'$filename'_input.txt"
mv -f "tests/output.txt" "scriptout/'$filename'_output.txt"
mv -f "tests/listing.txt" "scriptout/'$filename'_listing.txt"
mv -f "src/intermediate.txt" "scriptout/'$filename'_intermediate.txt"
done
You have single quotes around your variable names. Since they're inside double quotes, the variables will be expanded but the single quotes will be included in the filenames. Try this:
mv -f input.txt "scriptout/${filename}_input.txt"
The braces will protect the variable name from being combined with following characters.
You can do this:
list=(tests/*)
list=("${list[#]##*/}") # strip dirname
list=("${list[#]%.*}") # strip extension
These will prevent errors if there are spaces in any of the filenames.
Use indenting to make your script more readable:
for ...
do
command
done
In general, you should avoid using relative directories and use a variable as a base for an absolute directory:
basedir=/path/to/base
do_something $basedir/test/filename
mv $basedir/subdir1/file1 $basedir/subdir2
This can also make it so you don't have to use cd as much.
Are you getting any error messages? Try using set -x to turn on tracing and set +x to turn it off. Place those before and after particular sections of code so you can see what's happening in that area.

Categories

Resources