cd /D "%~dp0"
#echo off
for %%f in ("Z:\java_projects\IDS\trunk\deployment\config\client\%1\Documentation\MappingEngine_*.xml")
do(
echo %%~nxf
java -jar EngineUtil.jar 192.67.64.172 %%f
)
Please tell me what I'm doing wrong.
I want to create a batch file to loop through a set files from a directory and run a jar file with one file at a time passed as argument.
The directory is dependent on a command line parameter.
Related
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.
I'm trying to set up a Minecraft Server and actually everything works fine. I'm starting the Server with a launch.bat file.
"%ProgramFiles%\Java\jre1.8.0_20\bin\java.exe" -Xmx2G -Xms1G -jar Tekkit.jar nogui
pause
And I have a second .bat file for backup purposes.
SET COUNTER=0
:loop
SET /a COUNTER=%COUNTER%+1
XCOPY "Server\*" "c:\Users\Benjamin\Desktop\Tekkit Server\backups\server_backup_%COUNTER%" /i /s
PING 1.1.1.1 -n 1 -w 600000 > NUL
goto loop
Now, all I'm trying to do is to start the two .bat files simultaneously, using only one .bat file.
I tried it with a third .bat file to start all:
start cmd /k CALL "C:\Users\Benjamin\Desktop\Tekkit Server\Server\launch.bat"
start cmd /k CALL "C:\Users\Benjamin\Desktop\Tekkit Server\backup.bat"
pause
It will actually start both .bat files, but suddenly, they start acting up and won't find their own files again:
C:\Users\Benjamin\Desktop>"C:\Program Files\Java\jre1.8.0_20\bin\java.exe" -Xmx2G -Xms1G -jar Tekkit.jar nogui
Error: Unable to access jarfile Tekkit.jar
C:\Users\Benjamin\Desktop>pause
Drücken Sie eine beliebige Taste . . .
C:\Users\Benjamin\Desktop>SET COUNTER=0
C:\Users\Benjamin\Desktop>SET /a COUNTER=0+1
C:\Users\Benjamin\Desktop>XCOPY "Server\*" "c:\Users\Benjamin\Desktop\Tekkit
Server\backups\server_backup_1" /i /s
File * not found
0 File(s) copied
C:\Users\Benjamin\Desktop>PING 1.1.1.1 -n 1 -w 600000 1>NUL
All three .bat files are in different directories.
Does anybody have a clue what I could do about my problem (besides stop being lazy, trying to reduce 2 clicks down to one ;) )
Thanks a lot
Benny
The problem is most likely your path directory. when you do cmd, cd to the "right" directory first, then run the bat file. for example:
cmd /k "cd /my/app/path && app.bat"
the && is the separator for multiple commands
I tried to put two jar commands in a batch file, but it just won't work.
At first I tried this:
#echo off
"C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar Soulsplit.jar
"C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar Soulsplit.jar
I've also tried:
#echo off
"C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar Soulsplit.jar & "C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar Soulsplit.jar
if I run those batch files, they will just do one line, I hope someone could help!
It will only run the first command, I expect it to start up two programs.
Just putting one line of command:
#echo off
"C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar Soulsplit.jar
does work.
If you expect the output from your Soulsplit.jar to the command line, run java.exe instead of javaw.exe - see Difference between java.exe and javaw.exe.
I'm working on a batch file that recurs-es within a directory and called the Java Decompiler in order to decompile .class files.
For some reason or other, the files are decompiled but they don't remain in the original locations but instead the .jad files are in the location that I have called my bat file from.
My BAT file code is the following:
SETLOCAL EnableDelayedExpansion
FOR /D /r %%G in ("*") do (
#echo decompiling
jad -o "%%G/*.class"
#echo decompiled
#echo renaming
ren *.jad *.java
#echo renamed
)
Help would be appreciated.
It seems to me you don't understand the concept of the working directory all that well.
Your batch file is executed from a specific working directory, which than calls JAD continuously from there.
You need to either change your batch file to CD (Change directory), or to move the resulting decompiled files to the location where the .class was located.
Something like this should do it:
move *.java "%%G/"
Give it a try. Additionally, you should consider moving the .class files to another location if your planning on using the source code within a project.
Update
So your whole script for decompiling and keeping the original package structure should be the one below. I also took the liberty to add an option to delete the .class files for cleanup.
SETLOCAL EnableDelayedExpansion
FOR /D /r %%G in ("*") do (
#echo decompiling
jad -o "%%G/*.class"
#echo decompiled
#echo renaming
ren *.jad *.java
#echo renamed
#echo moving...
move *.java "%%G/"
#echo moved...
)
SET /P RESULT=[Delete .class files (y/n)]
IF %RESULT% == yes do (
FOR /D /r %%G in ("*") do (
cd "%%G/"
del *.class
)
)
In the latest version of Jad (1.5.8g) doesn't pickup filenames with certain characters (like a dollarsign) when using a wildcard. I've modified the script to decompile files individually to get around this limitation in Jad.
I've also modified it to use the -d option to specify the output folder. This removes the need to performe a move operation.
#echo off
SETLOCAL EnableDelayedExpansion
FOR /D /r %%G in ("*") do (
FOR %%X in ("%%G\*.class") do (
jad -o -d "%%G" "%%X"
ren "%%~dpnX.jad" "%%~nX.java"
)
)
SET /P RESULT=[Delete .class files (y/n)]
IF %RESULT% == yes do (
FOR /D /r %%G in ("*") do (
cd "%%G\"
del *.class
)
)
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