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

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.

Related

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.

Setting Java CLASSPATH doesn't work but -cp does

So, here's the problem I encountered. I wrote a simple .bat file to run weka on some data sets I had but Java recently updated itself and it stopped working. My old code was this:
#ECHO OFF
SET CLASSPATH = "C:\Program Files (x86)\Weka-3-6\weka.jar"
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI.arff
java weka.classifiers.meta.FilteredClassifier -t %%~nI.arff -F "weka.filters.unsupervised.attribute.Remove -R 1,3,4,5" -W weka.classifiers.functions.LinearRegression -x 10 >> results.txt
ECHO >> results.txt
)
This worked before and it did the job I asked of it. However, after the java update, I kept getting the error "Could not find or load main class weka.classifiers.meta.FilteredClassifier". I couldn't figure it out because the directory names and class names were exactly correct. So, I changed the code to this:
#ECHO OFF
SET CLASSPATH = "C:\Program Files (x86)\Weka-3-6\weka.jar"
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI.arff
java -cp "C:\Program Files (x86)\Weka-3-6\weka.jar" weka.classifiers.meta.FilteredClassifier -t %%~nI.arff -F "weka.filters.unsupervised.attribute.Remove -R 1,3,4,5" -W weka.classifiers.functions.LinearRegression -x 10 >> results.txt
ECHO >> results.txt
)
And it worked again. Can anyone tell me why this happened? The only thing I can think of is that the Java update isn't playing nice with itself somehow. Any insight would be appreciated thanks.
SET WEKA_HOME=c:\Program Files (x86)\Weka-3-6
SET CLASSPATH=%CLASPATH%;%WEKA_HOME%\weka.jar
bash learn.sh

How to find specific string text within direct .class file or .class file inside various jar file which are kept inside several sub-directories

My worries about System.out.println used in java code, all java files are not available for me. We have only .class in Production. There are thousand files having System.out.println entry.
In order to clean this string. How can I find all culprit class files which has this string.
I know javap, which disassemble the .class file. But don't know if javap can be used for my purpose.
Any Unix command or java programme or awk script or known UI tool will work for me.
I just want to get rid of System.out.println
I used below command in cygwin but no success !
find . -iname '*.class' -printf "%p | grep -q 'System.out.println' && echo %p\n" | sh
I am using Gnu grep version 2.10.
Then given a sample class file wich contatin System.out.println i get
bash$ grep 'System.out.println' sample.class
Binary file sample.class matches
so in order to extract the files with matches you could use something like:
#! /bin/bash
shopt -s globstar nocaseglob
files=(**/*.class)
for (( i=0; i<${#files[#]}; i++ )) ; do
file="${files[$i]}"
res=$(grep 'System.out.println' $file)
[[ $? == 0 ]] && awk '{print $3}' <<< "$res"
done

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.

Calling java program dependent on external library

I am trying to call a java program in php to use it with web interface.
Java program is dependent on an external lib: commons-cli-1.2.jar
So basically I need to export it before calling the java program; but if I export it first as:
shell_exec('export CLASSPATH=$CLASSPATH:~/lib/commons-cli-1.2.jar');
then call the java program as:
shell_exec('java ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o');
I think it creates different shells for each call; then the export does not have any effect on java program. Or am I wrong?
Otherwise, it should output a file in the server. But simply it does not. So, what is wrong? Any idea?
edit: However can it be because some parameters such as para_i stands for an input file name, so that i have to specify full path for that? Because I just assume if the input file is in the same working directory, there won't be any problem, will it?
edit-2: it outputs properly when i use command line;)
you're right, each shell_exec creates a separate shell.
env CLASSPATH=whatever java -switches
I would use
shell_exec('java -cp $CLASSPATH:/home/yourname/dir/lib/commons-cli-1.2.jar ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o > message');
and (this is important) replace the tilde(~) with the actual path to your directory (/home/yourname say). The ~ is expanded by the shell and is dependent on which shell you''re using.
Try Creating a simple shell script with the commands that you want to execute. You may pass arguments to a shell script so that is not a problem either.
for example
echo "Running Script..."
java -cp $CLASSPATH:~/lib/commons-cli-1.2.jar ComputePagerank -i $1 -d $2 -e $3 -o $4 > message
etc.
Then try calling it from the command line first with some parameters. Did it output? Then try calling it from the php script. Did it output? If it did not then you may need to check permissions. I had a simiolar experience some time ago with a Java program that simply did not have permission to write a file.
You should be able to call it like this.
shell_exec('java -cp $CLASSPATH:~/lib/commons-cli-1.2.jar ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o > message');
Another option is to issue the 2 commands seperately, but to the same shell, like this:
shell_exec('export CLASSPATH=$CLASSPATH:~/lib/commons-cli-1.2.jar; java ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o > message');
edit:
some shells don't let you call export while you're setting up the variable. so this may be safer than the second option above:
shell_exec('CLASSPATH=$CLASSPATH:~/lib/commons-cli-1.2.jar; export CLASSPATH; java ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o > message');
another edit:
If none of the above work then you're going to have to do some more trouble shooting. Does your java program work from the command prompt?
java -cp $CLASSPATH:/home/user/lib/commons-cli-1.2.jar ComputePagerank -i param1 -d param2 -e param3 -o param4 > message

Categories

Resources