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?
We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …] As we can see, in this case, we'll have to include the main class name in the command line, followed by arguments.
Open the Jar File wizard In the Package Explorer select the items that you want to export. If you want to export all the classes and resources in the project just select the project. Click on the File menu and select Export. In the filter text box of the first page of the export wizard type in JAR.
The jar command is a general-purpose archiving and compression tool, based on the ZIP and ZLIB compression formats. Initially, the jar command was designed to package Java applets (not supported since JDK 11) or applications; however, beginning with JDK 9, users can use the jar command to create modular JARs.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With