java -Duser.dir参数

本文详细介绍了Java中通过system.getProperties()获取系统信息的方法,并重点解释了user.dir属性的含义及应用场景,通过示例展示了如何获取当前用户所在目录。

在java中, system.getProperties()是获取系统定义的参数,用来获取系统信息。

user.dir:该属性表明当前用户所在的目录。

这样描述比较抽象,可以举个例子:

java代码:

public class Main { public static void main(String[] args) { System.out.println(System.getProperty("user.dir")); } }

打包:

jar cvf main.jar Main.class
将生成的jar包放到/home/madding/test目录下。

同时在该目录下建立一个简单的shel脚本:

#!/bin/bash echo "`pwd`" java -cp /home/madding/workspace/java/main.jar Main


运行方式:

在/home/madding/test目录下运行,输出:

/home/madding/test
/home/madding/test

在/home/madding目录下运行,输出:

/home/madding
/home/madding


这样也许更加容易理解user.dir的意思,即当前用户活动目录,和pwd目录一致。


所以当使用user.dir获取运行实例目录下的配置文件时,一定要注意目前用户所在的活动目录,要cd到指定目录后才调用脚步运行。




D:\JDK\bin\java.exe -Xmx700m -Djava.awt.headless=true "-Djna.boot.library.path=D:\实训\IntelliJ IDEA\IntelliJ IDEA 2025.2.1/lib/jna/amd64" -Djna.nosys=true -Djna.noclasspath=true --add-opens jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED -Dpreload.project.path=D:/新建文件夹/620 -Dpreload.config.path=C:/Users/13978/AppData/Roaming/JetBrains/IntelliJIdea2025.2/options -Dcompile.parallel=true -Drebuild.on.dependency.change=true -Didea.IntToIntBtree.page.size=32768 -Djdt.compiler.useSingleThread=true -Daether.connector.resumeDownloads=false -Dio.netty.initialSeedUniquifier=860575406354421900 -Djps.use.dependency.graph=true -Dfile.encoding=UTF-8 -Duser.language=zh -Duser.country=CN -Didea.paths.selector=IntelliJIdea2025.2 "-Djps.language.bundle=D:/实训/IntelliJ IDEA/IntelliJ IDEA 2025.2.1/plugins/localization-zh/lib/localization-zh.jar" "-Didea.home.path=D:/实训/IntelliJ IDEA/IntelliJ IDEA 2025.2.1" -Didea.config.path=C:/Users/13978/AppData/Roaming/JetBrains/IntelliJIdea2025.2 -Didea.plugins.path=C:/Users/13978/AppData/Roaming/JetBrains/IntelliJIdea2025.2/plugins -Djps.log.dir=C:\Users\13978\AppData\Local\JetBrains\IntelliJIdea2025.2\log\build-log "-Djps.fallback.jdk.home=D:/实训/IntelliJ IDEA/IntelliJ IDEA 2025.2.1/jbr" -Djps.fallback.jdk.version=21.0.8 -Dio.netty.noUnsafe=true -Djava.io.tmpdir=C:/Users/13978/AppData/Local/JetBrains/IntelliJIdea2025.2/compile-serve
10-14
D:\jdk\bin\java.exe -Xmx700m -Djava.awt.headless=true "-Djna.boot.library.path=D:\idea安装\IntelliJ IDEA Community Edition 2025.2.3/lib/jna/amd64" -Djna.nosys=true -Djna.noclasspath=true --add-opens jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED -Dexternal.project.config=C:\Users\y\AppData\Local\JetBrains\IdeaIC2025.2\projects\sims1.adf622b2\external_build_system -Dcompile.parallel=true -Drebuild.on.dependency.change=true -Didea.IntToIntBtree.page.size=32768 -Djdt.compiler.useSingleThread=true -Daether.connector.resumeDownloads=false -Dio.netty.initialSeedUniquifier=-5612769882385696046 -Djps.use.dependency.graph=true -Dfile.encoding=UTF-8 -Duser.language=zh -Duser.country=CN -Didea.paths.selector=IdeaIC2025.2 "-Djps.language.bundle=D:/idea安装/IntelliJ IDEA Community Edition 2025.2.3/plugins/localization-zh/lib/localization-zh.jar" "-Didea.home.path=D:/idea安装/IntelliJ IDEA Community Edition 2025.2.3" -Didea.config.path=C:/Users/y/AppData/Roaming/JetBrains/IdeaIC2025.2 -Didea.plugins.path=C:/Users/y/AppData/Roaming/JetBrains/IdeaIC2025.2/plugins -Djps.log.dir=C:\Users\y\AppData\Local\JetBrains\IdeaIC2025.2\log\build-log "-Djps.fallback.jdk.home=D:/idea安装/IntelliJ IDEA Community Edition 2025.2.3/jbr" -Djps.fallback.jdk.version=21.0.8 -Dio.netty.noUnsafe=true -Djava.io.tmpdir=C:/Users/y/AppData/Local/JetBrains/IdeaIC2025.2/compile-server/sims1_c0fff7a1/_
最新发布
10-23
构建进程终止异常: C:\Users\阿榆\.jdks\openjdk-22.0.2\bin\java.exe -Xmx700m -Djava.awt.headless=true "-Djna.boot.library.path=D:\java\IntelliJ IDEA 2024.1.4/lib/jna/amd64" -Djna.nosys=true -Djna.noclasspath=true --add-opens jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED -Dcompile.parallel=false -Drebuild.on.dependency.change=true -Didea.IntToIntBtree.page.size=32768 -Djdt.compiler.useSingleThread=true -Daether.connector.resumeDownloads=false -Dio.netty.initialSeedUniquifier=4218562153890322340 -Dfile.encoding=GBK -Duser.language=zh -Duser.country=CN -Didea.paths.selector=IntelliJIdea2024.1 -Djps.language.bundle=C:/Users/阿榆/AppData/Roaming/JetBrains/IntelliJIdea2024.1/plugins/zh.241.271/lib/zh.241.271.jar "-Didea.home.path=D:/java/IntelliJ IDEA 2024.1.4" -Didea.config.path=C:/Users/阿榆/AppData/Roaming/JetBrains/IntelliJIdea2024.1 -Didea.plugins.path=C:/Users/阿榆/AppData/Roaming/JetBrains/IntelliJIdea2024.1/plugins -Djps.log.dir=C:/Users/阿榆/AppData/Local/JetBrains/IntelliJIdea2024.1/log/build-log "-Djps.fallback.jdk.home=D:/java/IntelliJ IDEA 2024.1.4/jbr" -Djps.fallback.jdk.version=17.0.11 -Dio.netty.noUnsafe=true -Djava.io.tmpdir=C:/Users/阿榆/AppData/Local/JetBrains/IntelliJIdea2024.1/compile-server/java_se_f741f0a1/_temp_ -Djps.backward.ref.index.builder=true -Djps.backward.ref.index.builder.fs.case.sensitive=false "-Djps.kotlin.home=D:\java\IntelliJ IDEA 2024
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值