简体   繁体   English

Linux更改Java程序的工作目录

[英]Linux change working directory of java program

I want to run a java program in Linux and change its working directory to a specific location. 我想在Linux中运行Java程序并将其工作目录更改为特定位置。 I'm a bit of a noob with Linux, so I need quite a bit of help. 我对Linux有点菜鸟,所以我需要很多帮助。 I managed to figure this much out: 我设法弄清楚了:

java -jar program.jar

That will run the program in the working directory it chooses. 这将在其选择的工作目录中运行该程序。 Now I need to find a way to change the working directory. 现在,我需要找到一种更改工作目录的方法。 I think this computer is running UBuntu (or however you spell it) but I'm not sure. 我认为这台计算机正在运行UBuntu(或者您拼写了它),但是我不确定。 I'm also running on an account that has severely limited privileges. 我还使用特权严重受限的帐户运行。

EDIT (based on info given in asker's comment below): 编辑(基于下面asker注释中给出的信息):

So, it seems like you just need a batch script to cd into its own directory before launching java. 因此,似乎您只需要在运行Java之前将批处理脚本cd放入其自己的目录中即可。

Something like this script should do it. 像这样的脚本应该可以做到。

#!/bin/bash
DIR=`dirname "$BASH_SOURCE[0]"`
cd "$DIR"
echo "Current dir: $DIR"
javaw -jar program.jar &

So, make sure it's executable ( chmod +x minecraft.sh ), then when you double-click it or run the script from any folder, it will treat the script's folder as its working directory. 因此,请确保它是可执行文件( chmod +x minecraft.sh ),然后双击它或从任何文件夹运行脚本,它将脚本的文件夹视为其工作目录。 More information can be found in SO questions like this one . 此类问题中可以找到更多信息。

I'm not going to install Minecraft, otherwise my life will disappear down a dark gaming hole ;) 我不会安装Minecraft,否则我的生活将在一个黑暗的游戏洞中消失;)

Original answer: 原始答案:

say for desired working directory /x/y/z, and program.jar is in directory /a/b/c 说出所需的工作目录/ x / y / z,而program.jar位于目录/ a / b / c中

cd /x/y/z
java -jar /a/b/c/program.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM