I know there are many "unable to access jarfile" questions on here, but I do feel this is different enough to warrant its own thread.
I am writing a walkthrough, part of this walkthrough involves installing Cygwin and running a .jar file.
The problem is that this .jar file needs to be called from multiple directories, and rather than have my readers have to enter the full path to the .jar every time they need to run it, I would like them only to have to enter the .jar file command after having made a simple configuration to Cygwin.
I have tried adding the PATH to ~/.bashrc and have also tried adding the CLASSPATH, but have had no success.
Every time I invoke java -jar file.jar
I get Error: Unable to access jarfile file.jar
What should I do to resolve this?
[Edit]
I have spoken to my bro-in-law, who knows a bit about Linux, and he has suggested that I create a wrapper to execute the jar, I have had a quick search, but can't find anything simple.
Any suggestions?
Open File Explorer and the folder that includes your JarFile. Right-click the Jar file and select Open with then Choose another app. Select Java if it's listed among the default programs.
Encountering the “Unable to access jarfile /jar/minecraft_server. jar” error occurs when the server panel is unable to locate the designated JAR File that has been set. This can occur if the file has an incorrect name or if the original file was deleted for any reason.
Cygwin already provides a way to convert between UNIX (POSIX)-style paths and Windows (MS-DOS)-style paths, it's called "cygpath".
Try:
java -jar `cygpath -w ./foo/bar/file.jar command file.1 file.2`
Note: Those are backticks surrounding the cygpath invocation.
Using cygpath eliminates the need to write your own bash script to pass things to, and the jar file can be located anywhere on your computer.
For example, to make a bash script that minifies JavaScript code using Google's Closure Compiler in an bash environment-agnostic way:
#!/bin/bash
# Script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Path to Closure Compiler
COMPILER=$HOME/archive/closure-javascript.jar
# Java requires converting paths from UNIX to Windows under Cygwin
case "$(uname -s)" in
CYGWIN*) COMPILER=$(cygpath -w $COMPILER)
esac
# Java must be in the PATH
COMMAND="java -jar $COMPILER"
# Allow overriding default filename from the command line
cd $SCRIPT_DIR
rm *.min.js > /dev/null 2>&1
# Minify all files in the directory
for js in *.js; do
$COMMAND --js=$js --js_output_file=$(basename $js .js).min.js
done
This script should execute correctly under Cygwin and Linux.
java -jar file.jar works only if file.jar is in your current working directory. If not, you need to provide the path to file.jar. Note that if you're using Oracle Java, you need to be aware that it is a native Windows program and does not understand Cygwin's *NIX paths.
As for your brother-in-law's advice, he is correct that Java programs are usually launched via wrapper scripts on *NIX platforms. If you're using Oracle Java, that would look like:
#! /bin/sh
exec java -jar $(cygpath -w /path/to/file.jar) "$@"
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