I have written a Java program for linux and windows.
To start the program, I use the following code in linux:
abc.sh:
#!/bin/bash
java -jar /opt/AudiobookConverter/AudiobookConverter.jar "$#"
The "$#" will expand any wildcard I may throw at it.
So, if I want my program to process all .mp3-files in a certain directory, I just call abc.sh *.mp3 and it does its magic.
Under Windows, I have the following code, which is supposed to do the same:
abc.bat:
java -jar C:\AudiobookConverter\AudiobookConverter.jar "%*"
But when I call abc.bat *.mp3, it will pass *.mp3 to the java program, instead of a list of the files ending in .mp3.
What am I doing wrong, and how to fix this?
Greetings,
AHahn94
#echo off
setlocal enabledelayedexpansion
set "files="
for /f "delims=" %%a in ('dir /b /a-d "%*" ') do set "files=!files! %%a"
java -jar C:\AudiobookConverter\AudiobookConverter.jar "%files%"
The setlocal command opens a local environment with delayed expansion invoked. The dir command lists files (names only - no directorynames), the for assigns the entire list-line to %%a, and each name is appended to the environment variable files using the delayed expansion syntax to access the run-time value.
Once the files variable has been established, conventional syntax may be used to pass it to the java command.
Related
I have written a script to change the java environment variables of the shell:
#!/bin/bash
#env variables can be changed only if we call the script with source setJavaVersion.sh
case $1 in
6)
export JAVA_HOME=/atgl/product/java/jdk-1.6.0_43/linux-redhat_x86_64/jdk1.6.0_43/
export PATH=$JAVA_HOME:$PATH ;
;;
7)
export JAVA_HOME=/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51
export PATH=$JAVA_HOME:$PATH ;
;;
8)
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64/jre/
export PATH=$JAVA_HOME:$PATH ;
;;
*)
error java version can only be 1.6, 1.7 or 1.8
;;
esac
To execute it, I enter:
source setJavaVersion.sh 6
to set the environment with jdk6, source setJavaVersion.sh 7 for jdk7 and so.
when I look at the environment variables with:
$ echo $JAVA_HOME
and
$ echo $PATH
I see that the variables are well updated.
However, when I execute the command
java -version
it is not updated.
If I enter the same export commands directly in the shell, java -version returns the updated result.
Why ?
Edit:
I have updated my script with the deathangel908 answer.
Here is the output of which java and PATH before and after the script execution:
$ which java
/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin/java
$ echo $PATH
/CPD/SDNT/tools/bin:/CPD/SDNT/tools/x86_64-pc-unix11.0/bin:/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin:/CPD/SDNT/tools/bin:/CPD/SDNT/tools/x86_64-pc-unix11.0/bin:/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/users/t0059888/bin:/users/t0059888/bin
$ source setJavaVersion 6
$ which java
/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin/java
$ echo $PATH
/CPD/SDNT/tools/bin:/CPD/SDNT/tools/x86_64-pc-unix11.0/bin:/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin:/CPD/SDNT/tools/bin:/CPD/SDNT/tools/x86_64-pc-unix11.0/bin:/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/users/t0059888/bin:/users/t0059888/bin:/atgl/product/java/jdk-1.6.0_43/linux-redhat_x86_64/jdk1.6.0_43/
You're appending path every time, you need to remove it and add again
export
: export a="$a:3"
$ echo $a # :3
: export a="$a:3"
: echo $a # :3:3
When you executing java, bash starts lookup in PATH variable, finds the first occurrence and executes it.
You can use which java to check the real path of java command that's executed.
So to solve your issue just remember the path w/o java:
if [ -z ${PATH_SAVE+x} ]; then
export PATH_SAVE="$PATH"
fi
export PATH="$PATH_SAVE:$JAVA_HOME"
Remember to quote variables, in case they contain special symbols or spaces.
Also you can debug your script by running echo $PATH
Based on the output you added in your Edit, the new PATH was added at the end. Since Java 7 is at the beginning of your PATH that one is used when you run which java.
When you execute a command, the first occurrence found on on the PATH will be used, so, try adding it at the beginning of the variable (as you did in your original script, without the changes proposed in the other answer. I mean, it is a good idea what the other answer suggested, that you should not be appending the same paths over and over again, but if you add the Java path at the end of your PATH variable, make sure no other java is found on a previous path).
For what I can see in your original script, it should be working fine.
Try adding set -x at the beginning of your original script, and look at the output. It would be helpful if you could share that output as well.
Finally, make sure the binaries in Java 6 have the right file permissions (make sure java is executable).
My error was that the java executable was not accessible in the path. It is located in the bin folder. What I did before was wrong:
export PATH="$JAVA_HOME:$PATH"
This is the solution:
export PATH="$JAVA_HOME/bin:$PATH"
I have a Java jar file located in:
C:\Users\myusername\bin\MyDir\MyApp.jar
I also have some required properties files (needed as input arguments to the .jar file) located in the same directory as the .jar file.
I created a runme.bat file here:
C:\Users\myusername\Desktop\runme.bat
In the runme.bat file, this is what I have:
setlocal
set JAVA_HOME="C:\Program Files\Java\jdk1.8.0_161\bin\"
set PATH=C:\Users\myusername\bin\MyDir\
start %JAVA_HOME%javaw -jar %PATH%MyApp.jar %PATH%propertiesfile.properties
However, whenever I try to run the .bat file, I get the error:
Windows cannot find '-jar' Make sure you typed the name correctly, and then try again.
On the command line I see Windows trying to do this:
> "C:\Program Files\Java\jdk1.8.0_161\bin\"javaw -jar C:\Users\myusername\bin\MyDir\MyApp.jar ...
I get this error when running from the command line. If I simply double-click the .bat file, a cmd window comes up and quickly disappears.
So, what am I doing wrong?
Thanks!
Use Double quotes around the set command, not inside the variables.
Also, I see no reason to use the START command unless you want to do more in your batch file in the original command prompt after starting your Java in a second command prompt. Possible but seems unlikely.
Generally, you will just type in the executable or use CALL so that the executable runs and control returns to the batch after reaching conclusion.
Additionally, you changed your system path variable to be just the path of your java files which will make the session pretty screwy. Thankfully this should only persist in your open command windows and those spawned by the original window, so close them all and then use a different variable name for your path.
So I will put this both ways, using Call, and using start.
Here is your code using call:
#(
setlocal
ECHO ON
)
set "_Title=Runnning My Java"
set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_161\bin"
set "_MyJarPath=C:\Users\%UserName%\bin\MyDir"
TITLE "%_Title%"
CD /D "%JAVA_HOME%"
CALL "%JAVA_HOME%\javaw.exe" -jar "%_MyJarPath%\MyApp.jar" "%_MyJarPath%\propertiesfile.properties"
(
ENDLOCAL
EXIT /B 0
)
Here is your code using the start command:
#(
SETLOCAL
ECHO ON
)
set "_Title=Runnning My Java"
set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_161\bin"
set "_MyJarPath=C:\Users\%UserName%\bin\MyDir"
start "%_Title%" /D "%JAVA_HOME%" "%JAVA_HOME%\javaw.exe" -jar "%_MyJarPath%\MyApp.jar" "%_MyJarPath%propertiesfile.properties"
(
ENDLOCAL
EXIT /B 0
)
I am trying to port a simple shell script starting a Java program to a CMD batch script for Windows:
#echo on
set REPO="C:\Users\user1\.m2\repository"
set VERSION=9.3.9.v20160517
"C:\Program Files\Java\jdk1.8.0_66\bin\java.exe" -classpath C:\Users\user1\slova\WebSockets\target\classes;%REPO%\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;%REPO%\org\eclipse\jetty\websocket\websocket-server\%VERSION%\websocket-server-%VERSION%.jar;%REPO%\org\eclipse\jetty\websocket\websocket-common\%VERSION%\websocket-common-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-io\%VERSION%\jetty-io-%VERSION%.jar;%REPO%\org\eclipse\jetty\websocket\websocket-client\%VERSION%\websocket-client-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-servlet\%VERSION%\jetty-servlet-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-security\%VERSION%\jetty-security-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-server\%VERSION%\jetty-server-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-http\%VERSION%\jetty-http-%VERSION%.jar;%REPO%\org\eclipse\jetty\websocket\websocket-servlet\%VERSION%\websocket-servlet-%VERSION%.jar;%REPO%\org\eclipse\jetty\websocket\websocket-api\%VERSION%\websocket-api-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-util-ajax\%VERSION%\jetty-util-ajax-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-util\%VERSION%\jetty-util-%VERSION%.jar;%REPO%\org\postgresql\postgresql\9.4.1208.jre7\postgresql-9.4.1208.jre7.jar de.afarber.websockets.MyHandler
As you can see (if you scroll the above code to the right) there is a longer list of file paths following the java -classpath string.
Is it please possible to list the paths each on a separate line - and then concatenate that list by the means of CMD shell to a variable (adding semicolon ; inbetween)?
That way I could better maintain my batch file (easier to edit in editor) and would finally just call java -classpath %CPATHS% de.afarber.websockets.MyHandler
UPDATE:
If all JAR-files would be located in the same dir, I could have used the new Java 8 wildcard syntax java -classpath "\that\dir\*" de.afarber.websockets.MyHandler - but that wasn't the case here.
You can use a variable in this form:
set MYCLASSPATH=C:\Users\user1\slova\WebSockets\target\classes
set MYCLASSPATH=%MYCLASSPATH%;%REPO%\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar
...
"C:\Program Files\Java\jdk1.8.0_66\bin\java.exe" -classpath %MYCLASSPATH% ...
I have a number of weka datasets I need to run at once so I was creating a batch file to do that.
#ECHO OFF
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI
SET CLASSPATH=C:\Program Files (x86)\Weka-3-6\weka.jar
java weka.classifiers.functions.LinearRegression -t %%~nI -x 10
)
When I run the SET CLASSPATH command and the java command in a regular command line they work fine, and they also work on their own in a batch file but as soon as I nested them in a for loop, I started getting "\Weka-2-6\weka.jar was unexpected at this time" errors.
I'm not an expert in Java or in Batch file programming so forgive me if the fix is really simple but I've been up and down the internet and I haven't found any solutions to this problem. What am I doing wrong here?
Thanks for your help.
#ECHO OFF
setlocal
SET "CLASSPATH="C:\Program Files (x86)\Weka-3-6\weka.jar""
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI
java weka.classifiers.functions.LinearRegression -t %%~nI -x 10
)
endlcoal
set class path can be expanded after the for loop is finished.So better defined it outside the loop.
In a bash shell script I tried these two versions:
java -jar abc.jar&
and
CMD="java -jar abc.jar&"
$CMD
The first verison works, and the second version complains that abc.jar cannot be found. Why?
Commands do run from current directory in a shell script.
This is why the first command in your test script worked.
The second command may not work because either java isn't in your ${PATH} or abc.jar isn't in your ${CLASSPATH}. You could echo these environment variables or set +x to debug your bash script.
Bash (and others) won't let you do backgrounding (&) within the value of a variable (nor will they let you do redirection that way or pipelines). You should avoid putting commands into variables. See BashFAQ/050 for some additional information.
What is the actual error message you're getting? I bet it's something like "abc.jar& not found" (note the ampersand) because the ampersand is seen as a character in the filename.
Also, the current directory for the script is the directory that it is run from - not the directory in which it resides. You should be explicit about the directory that you want to have your file in.
java -jar /path/to/abc.jar&