Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an alias for a .jar command-line script?

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?

like image 281
Paul Patterson Avatar asked Jun 17 '16 13:06

Paul Patterson


People also ask

How do you call a JAR file from the command line?

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.

What is the command to create a JAR file?

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.

What is the jar command line?

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.


1 Answers

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?

like image 100
l'L'l Avatar answered Oct 08 '22 00:10

l'L'l