0

I'm doing this in my makefile to try and get the PATH to Java, but the resulting string from find isn't escaped properly to be used in bash, how do I get find to return the escaped path to java.exe?

JAVA_EXE := "$(shell find /c/Program\ Files\ \(x86\)/Java/ -type f -name 'java.exe' -print0 -quit)"
JAVA_PATH := $(dir $(JAVA_EXE))
oguz ismail
  • 1
  • 16
  • 47
  • 69
  • You sure you need it to be escaped? You can directly write to a makefile variable named `PATH`, then do `export PATH` to turn it into an environment variable. – HolyBlackCat Mar 14 '21 at 18:39
  • JAVA_EXE contains the location of the file non-escaped and usable to execute, but I need the path to the file not the file itself – Paul Douglas Mar 14 '21 at 18:45
  • I get it, but what are you planning to do with the path? Do you want to add it to the `PATH` variable? – HolyBlackCat Mar 14 '21 at 18:49
  • Yes, for the path but I need two distinct variables. One to the executable and another *for adding to path* – Paul Douglas Mar 14 '21 at 18:51
  • Then my first comment applies. You might not need to escape it to add it to the `PATH`. – HolyBlackCat Mar 14 '21 at 18:53
  • You are probably correct, but I definitely have to do something with it, because it contains the filename java.exe at the moment – Paul Douglas Mar 14 '21 at 18:54
  • Didn't you remove the filename using `$(dir ...)`? – HolyBlackCat Mar 14 '21 at 18:59
  • I tried, but that doesn't work because the filename is invalid in bash (i.e. contains spaces and ( and ) which are unescaped – Paul Douglas Mar 14 '21 at 19:03
  • Do not add quotes around the results of the `$(shell ...)` command. That is wrong. See https://stackoverflow.com/questions/23330243/gnu-makefile-how-to-and-when-to-quote-strings/23332194#23332194 – MadScientist Mar 14 '21 at 19:05
  • The short answer is, you cannot use paths containing spaces in makefiles. You definitely cannot use them with any make functions like `dir`, etc. Sorry about that. – MadScientist Mar 14 '21 at 19:07
  • @MadScientist I don't believe that is true if I can get an escaped answer from find – Paul Douglas Mar 14 '21 at 19:09
  • `dir` is a make function. Make has no facility for escaping whitespace. There's no way to get the make function `$(dir ....)` to work the right way, if `...` is a path containing whitespace. – MadScientist Mar 14 '21 at 20:02
  • I can remove the java.exe with JAVA_PATH := $(subst java.exe,,$(JAVA_EXE)) This still doesn't answer my main question which is how to get an escaped directory from find... – Paul Douglas Mar 15 '21 at 01:44

1 Answers1

0

Ok, so I've learned a lot since I last posted this question.

I needed a script, like so called addJavaToPath.sh

#!/bin/sh
JAVA_EXE=$(find "/c/Program Files" -type f -name java.exe -print -quit 2>/dev/null)
echo JAVA_EXE: $JAVA_EXE
JAVA_PATH=${JAVA_EXE%'java.exe'}
echo JAVA_PATH: $JAVA_PATH
export PATH=$PATH:"$JAVA_PATH"

and then you type:

source addJavaToPath.sh

it needs to be sourced because you want to mess with path and normally that export would only last for the life of the script