You are not logged in.

#1 2013-08-06 06:24:21

laite
Member
Registered: 2011-06-05
Posts: 33

[Solved] [Bash] Running java with space in the argument

Hi. I think this should be pretty easy, but I just can't. Hopefully somebody could help me.

Simply put: I want to run java command where the argument has a space in filename.

This is my script:

#!/bin/bash

TESTPATH="/home/laite/bin/test dir"
EXEC="java -jar \"$TESTPATH\"/some.js"

echo $EXEC
${EXEC}

and the output:

# laite@laite-arch ~/bin
> ./java_test.sh 
java -jar "/home/laite/bin/test dir"/some.js
Error: Unable to access jarfile "/home/laite/bin/test

Last edited by laite (2013-08-06 13:44:05)

Offline

#2 2013-08-06 07:34:12

andjeng
Member
From: Indonesia
Registered: 2012-08-30
Posts: 146

Re: [Solved] [Bash] Running java with space in the argument

you should use TESTPATH="/home/laite/bin/test\ dir"

EDIT : i've found this http://stackoverflow.com/questions/1214 … -as-a-java

Last edited by andjeng (2013-08-06 08:08:42)


just looking around. wink

Offline

#3 2013-08-06 11:50:04

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,794
Website

Re: [Solved] [Bash] Running java with space in the argument

EXEC="java -jar \"${TESTPATH}/some.js\""

InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#4 2013-08-06 13:13:18

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,097
Website

Re: [Solved] [Bash] Running java with space in the argument

Your first line says you're using bash, so... use bash features?

cmd=(java -jar "$jarpath"/some.jar)
printf '%q ' "${cmd[@]}"; echo
"${cmd[@]}"

Offline

#5 2013-08-06 13:43:43

laite
Member
Registered: 2011-06-05
Posts: 33

Re: [Solved] [Bash] Running java with space in the argument

falconindy wrote:

Your first line says you're using bash, so... use bash features?

cmd=(java -jar "$jarpath"/some.jar)
printf '%q ' "${cmd[@]}"; echo
"${cmd[@]}"

Nice, this worked! Thanks.

Edit: And just now I understood why and how it worked. Snappy tongue

Last edited by laite (2013-08-06 16:28:28)

Offline

Board footer

Powered by FluxBB