7

I've got a jar file that can be run from the command line like this:

# In Terminal.app
java -jar fop.jar /path/to/infile.fo /path/to/outfile.pdf

At the moment for this to work I need to navigate to the folder containing fop.jar. This isn't ideal, so I've tried to add the following alias to bash_profile...

# In bash_profile
alias fop="java -jar /path/to/a/far/away/folder/fop-2.1/build/fop.jar"

...with the hope that I could execute this script from the Desktop (or anywhere else) like so:

# In Terminal
fop ~/Desktop/simple.fo ~/Desktop/simple.pdf 

Unfortunately it's not working:

# Error message
Unable to start FOP:
java.lang.RuntimeException: fop.jar not found in directory: /my/pwd (or below)
    at org.apache.fop.cli.Main.getJARList(Main.java:70)
    at org.apache.fop.cli.Main.startFOPWithDynamicClasspath(Main.java:130)
    at org.apache.fop.cli.Main.main(Main.java:219)

Can anyone help?

martinez314
  • 12,162
  • 5
  • 36
  • 63
Paul Patterson
  • 6,840
  • 3
  • 42
  • 56

1 Answers1

1

An alias or a function should work fine as long as the real path of java and fop.jar is used:

$ which java
/usr/bin/java

If you wanted to create a function (which has the benefit of allowing you to change options, parameters, arguments, etc.) you could add something such as this to ~/.bash_profile:

# fop: run java -jar with input and output files
fop () { /usr/bin/java -jar /path/to/fop.jar "$1" "$2" ; }

The key ingredient is telling the function or alias the path of your java executable and fop.jar. Another thing that might help resolve the issue is to create a symlink to fop.jar someplace convenient, then you can easily reference it as needed.

ln -s /path/to/fop.jar /path/to/symlink

So by doing that your command would essentially would become:

/usr/bin/java -jar /path/to/symlink infile outfile

In Bash, when to alias, when to script, and when to write a function?

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
l'L'l
  • 44,951
  • 10
  • 95
  • 146