Unable to access jarfile under cygwin

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

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?

回答1:

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.



回答2:

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) "$@" 


回答3:

OK, this has now been resolved.

I have managed to create a bash script that calls the .jar and it's arguments, and thanks to Bernie Zimmerman's Blog i have managed to get it to run.

The issue appears to be that while Cygwin is a Linux environment using Posix filenames, Java still wants windows filenames.

The .jar in question has 2 possible commands, and enacts these commands on a file and creates another file.

So I can now replace

java -jar ./foo/bar/file.jar command file.1 file.2

with

jar.sh command file.1 file.2

the bash script I created is:

#!/bin/bash
java -jar "C:\foo\bar\file.jar" "$1" "$2" "$3"



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!