如何指示 macOS 可执行文件从当前目录获取 JAR 文件。

huangapple 未分类评论45阅读模式
英文:

How to tell mac executable to pick up jar from current directory

问题

我有一个Mac可执行文件,其中包含以下内容:

#! /bin/bash
java -jar <absolute_path>/jar_file.jar

如果我们不在上面提到绝对路径,据我所知,它将在Mac用户目录中搜索并显示"无法访问jar"错误。因此,为了避免这种情况,我们提供绝对路径,但是当您将可执行文件移动到另一台机器时,绝对路径将无法帮助。

那么,如何使可执行文件在任何位置都能正常工作,而无需每次移动时修改上述路径

英文:

I have Mac executable that has below lines

#! /bin/bash
java -jar &lt;absolute_path&gt;/jar_file.jar

If we don't mention absolute path above, As I know it will search in mac user dir and will give error as "unable to access jar", So to avoid this we give absolute path, but absolute path will not help when you move the executable to another machine.

So how to make executable work at any place without modifying above path every time we move

答案1

得分: 0

你可以从mvn脚本中获取适当的代码:

## 解析链接 - $0 可能是指向Maven主目录的链接
PRG="$0"

# 针对相对符号链接进行处理
while [ -h "$PRG" ] ; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG="`dirname "$PRG"`/$link"
  fi
done

saveddir=`pwd`

PRG 将等于 "<绝对路径>/executable.bash";JAR 文件将位于

`dirname $PRG`/jar_file.jar
英文:

You can steal the appropriate code from the mvn script:

## resolve links - $0 may be a link to Maven&#39;s home                             
PRG=&quot;$0&quot;                                                                        
                                                                                
# need this for relative symlinks                                               
while [ -h &quot;$PRG&quot; ] ; do                                                        
  ls=`ls -ld &quot;$PRG&quot;`                                                            
  link=`expr &quot;$ls&quot; : &#39;.*-&gt; \(.*\)$&#39;`                                            
  if expr &quot;$link&quot; : &#39;/.*&#39; &gt; /dev/null; then                                     
    PRG=&quot;$link&quot;                                                                 
  else                                                                          
    PRG=&quot;`dirname &quot;$PRG&quot;`/$link&quot;                                                
  fi                                                                            
done                                                                            
                                                                                
saveddir=`pwd`

PRG will equal "<absolute_path>/executable.bash"; the JAR will be at

`dirname $PRG`/jar_file.jar

答案2

得分: 0

在Unix系统中,波浪号(~)字符是用户主目录的快捷方式。
可以使用~字符来替代用户主目录的绝对路径。

使用~的工作示例:

#!/bin/bash
java -jar 〜/jar_file.jar

用户主目录在不同系统上的绝对路径可能不同,
但上述脚本中的~将解析为正确的用户主目录路径。

英文:

In Unix systems, the tilde (~) character is a shortcut for user's home directory.
The ~ character can be used to replace the absolute path to user's home directory.

Working Example using ~ :

#! /bin/bash
java -jar ~/jar_file.jar

The user home can have different absolute paths on different systems,
but ~ in the above script will resolve to the the correct user home directory path.

huangapple
  • 本文由 发表于 2020年7月25日 18:40:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/63087334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定