简体   繁体   中英

ArrayIndexOutOfBounds Exception on executing linux sh file

I have a program in java which takes 0'th aargument as file location like

File f = new File(args[0]);

so when i execute it using a windows batch(.bat) file it works correctly . but when i execute the same using a linux shell file(.sh) in linux i get ArrayIndexOutOfBoundsException.

WINDOWS BATCH FILE :

@echo off
for /f %%i in ("%0") do set scriptpath=%%~dpi
set cp=%scriptpath%/../lib/*.jar;
java -classpath %cp% com.synchronizer.main.MYSynchronizer %scriptpath% "%1" "%2"

LINUX SH FILE:

export JAVA_HOME=/usr/local/java
PATH=/usr/local/java/bin:${PATH}

THE_CLASSPATH=
for i in `ls ../lib/*.jar`
do
  THE_CLASSPATH=${THE_CLASSPATH}:${i}
done

java -cp ".:${THE_CLASSPATH}"  \
com.synchronizer.main.MYSynchronizer

please help!

It looks like a problem in script (no arguments are passed to the Java program).

You can consider to debug the script like this: debugging scripts

Hope this helps

Your shell script is not passing any parameters:

java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer

Try:

java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer "$1" "$2"

As stated above, your Linux shell script is not sending any arguments to the Java program that you are trying to start.

And, adding to that, you are not showing us how you run the Linux shell script. If no argument is given on the command line when you start the shell script, no arguments can be passed to your Java application from the shell script.

If you want to see the actual command that is going to be run by your shell script, you can always put "echo" in front of a line and see what all variables are expanded to. This is a simple way to debug shell scripts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM