Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jammin0

macrumors newbie
Original poster
Sep 27, 2010
6
0
I'm trying to create a shell script or compile it into an application using automator that runs a JAR file. Everything works great except that I have to specify the full pathname in the shell script. I thought that ./ or .../ or something similar would denote that I want to run the .JAR file that is in the containing folder but I'm not having any luck.

Anyone know if this is possible?
 
Details are lacking and honestly some of what you said doesn't make sense.

Despite that, ./ only works if the script is in your working directory. The working directory in automator is the root of your home folder.

So, if the script is in your home folder, you can work relatively from your home folder. Your best bet is to define a path variable in the beginning of your script. That way, if things move, you can easily change that variable.
 
I'm trying to create a shell script or compile it into an application using automator that runs a JAR file. Everything works great except that I have to specify the full pathname in the shell script. I thought that ./ or .../ or something similar would denote that I want to run the .JAR file that is in the containing folder but I'm not having any luck.

Anyone know if this is possible?

As indicated above, the ../../ trick won't always work (reliably) from within a script.

This perhaps...

In a Bash shell script, we can locate its parent via:

parent=$(cd "${0%/*}" && pwd -P)

... so i guess your jar file actually lives in the grandparent then?

grandpa=$(dirname "$parent")

...making the full path to it something like:

$grandpa/
your.jar


EDIT: Unless by "containing folder" you mean the script and the jar are in the same directory... in which case you can skip the grandparent stuff, and go directly to:

$parent/
your.jar


EDIT2: A shorter way to get the script's parent is: parent=`dirname $0`... but that doesn't always produce its "full" pathname (e.g., like when a script is run via ./scriptname thus bypassing the $PATH mechanism).
 
Thanks for the replies. Let me try and explain. I apologize I am a long DOS user since over 15 years ago and my unix/mac os experience is limited.

So I think Hal understood better what I am trying to do. I have a folder that contains a shell script whose function is simply to run a JAR file. It works great if I input the entire filename into the script.

I would like to use this script on multiple machines with multiple users without having to change the filename in each script.

What I said about Automator previously was not really relevant as I'm only using automator so that I can have an application file instead of having terminal run at the same time, it is going to confuse some of the users and I don't want the program to terminate because someone closes the terminal windows.

Example:
Working Script
#!/bin/bash
java -jar -Dsun.java2d.noddraw=true /users/admin/downloads/my jar.jar

What would work in DOS environment assuming that the jar and the script were in the same parent folder but doesn't work here:
#!/bin/bash
java -jar -Dsun.java2d.noddraw=true my jar.jar

Hal you might have understood that but your explanation was a little bit beyond me. I think I already know the location of the file, I just want to not have to input it.

Thanks in advance for any help, this forum is awesome.
 
Hal you might have understood that but your explanation was a little bit beyond me. I think I already know the location of the file, I just want to not have to input it.

What more can i say?
Once you put this line in your script...

parent=$(cd "${0%/*}" && pwd -P)

...then the variable "$parent" becomes the full path to the folder containing the script (no matter where it is, on any machine).

So consequently (presumably),

$parent/your.jar

is the full path to your jar file.

[need to use the actual name in place of "your" obviously, and follow normal Unix convention regarding quoting or escaping if there are any spaces (or weird chars) in its name.]


EDIT: one could add in some extra sanity checks, and try to anticipate errors i suppose...
Code:
parent=$(cd "${0%/*}" >/dev/null && pwd -P)
[[ -d $parent ]] || {
	echo "oops... '$parent' isn't a folder." >&2
	exit 1
}

If you're uncertain, use echo to test what the various vars look like.
 
I'm trying to create a shell script or compile it into an application using automator that runs a JAR file. ...
You seem to going all around Job's barn to do something that can also be done by simply double-clicking on the .jar file. Or am I missing something?
 
Good point but I need to specify other variables when I run it like in my example code. I am not simply running the JAR file or else you would be correct.
 
Finally figured out to use the Jar bundler instead of automator. I could never get your code to work Hal it must have been the fact that I was running the shell script from inside automator. Oh well, the jar bundler let me run the extra parameters that I need and packaged it up nicely as an application that I can run from wherever.

Thanks everyone for the suggestions and help
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.