如何在Java程序中运行.jar文件? [英] How to run a .jar file from within Java program?

查看:1525
本文介绍了如何在Java程序中运行.jar文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想在这里做的是运行.jar文件,该文件位于

What I'm basically trying to do here, is run a .jar file which is located under

C/Users/-any user here-/appdata/Roaming/-my folder here-/-file name here-.jar

我是否以某种方式打开CMD并执行:

Do I somehow open a CMD and do:

cd appdata/Roaming/<Folder>
java -jar <FileName>.jar

当我输入它时这似乎有效进入CMD本身。从java程序运行时,我似乎无法使它工作。

This seems to work when I type it into CMD itself. I can't seem to make it work when running from java program.

我试图这样做:

Runtime.getRuntime().exec("cd appdata/Roaming");

我收到指定目录不存在的错误。

And I get an error that the specified directory doesn't exist.

推荐答案

使用绝对路径而不是相对路径,如果从任何工作目录运行,应该阻止找不到路径。否则将其添加到您的类路径中,如Nizil所说。

Use an absolute path instead of a relative path, that should prevent the path not being found if you run from any working directory. Otherwise add it to your classpath as Nizil said.

要获取当前用户的名称,请使用 System.getProperty(user.name)并连接到你的路径。

To get the current user's name, use System.getProperty("user.name") and concatenate into your path.

user = System.getProperty("user.name");
cmd = "java -jar C/Users/" + user + "/appdata/Roaming/<folder>/<file>.jar";
Runtime.getRuntime().exec(cmd);

这篇关于如何在Java程序中运行.jar文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆