gpt4 book ai didi

java - 如何从 jar 文件在 linux 命令行中执行 yum 安装?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:24 25 4
gpt4 key购买 nike

我希望能够在 linux 终端中执行一个 jar 文件,该文件将运行一系列命令以 yum 在服务器上安装多个程序。目标是使一些安装过程自动化,因为我们进行了如此多的安装以进行测试。我有以下代码:

public class test 
{
public static void main(String[] args)
{
Runtime rt = Runtime.getRuntime();
String cmd;

cmd = "/bin/sh -c yum install -y rpm-build";
rt.exec(cmd);
}
}

当我运行它时,没有任何反应。如果我只是运行“yum install -y rpm-build”它工作正常,但如果我运行 jar 来执行它则不行。我做错了什么?

来自以下代码/评论的错误流:

    root@netBoot labStats]# java test 
Loaded plugins: fastestmirror
Usage: yum [options] COMMAND

List of Commands:

check Check for problems in the rpmdb
check-update Check for available package updates
clean Remove cached data
deplist List a package's dependencies
distribution-synchronization Synchronize installed packages to the latest available versions
downgrade downgrade a package
erase Remove a package or packages from your system
groupinfo Display details about a package group
groupinstall Install the packages in a group on your system
grouplist List available package groups
groupremove Remove the packages in a group from your system
help Display a helpful usage message
history Display, or use, the transaction history
info Display details about a package or group of packages
install Install a package or packages on your system
list List a package or groups of packages
load-transaction load a saved transaction from filename
makecache Generate the metadata cache
provides Find what package provides the given value
reinstall reinstall a package
repolist Display the configured software repositories
resolvedep Determine which package provides the given dependency
search Search package details for the given string
shell Run an interactive yum shell
update Update a package or packages on your system
upgrade Update packages taking obsoletes into account
version Display a version for the machine and/or available repos.


Options:
-h, --help show this help message and exit
-t, --tolerant be tolerant of errors
-C, --cacheonly run entirely from system cache, don't update cache
-c [config file], --config=[config file]
config file location
-R [minutes], --randomwait=[minutes]
maximum command wait time
-d [debug level], --debuglevel=[debug level]
debugging output level
--showduplicates show duplicates, in repos, in list/search commands
-e [error level], --errorlevel=[error level]
error output level
--rpmverbosity=[debug level name]
debugging output level for rpm
-q, --quiet quiet operation
-v, --verbose verbose operation
-y, --assumeyes answer yes for all questions
--version show Yum version and exit
--installroot=[path] set install root
--enablerepo=[repo] enable one or more repositories (wildcards allowed)
--disablerepo=[repo] disable one or more repositories (wildcards allowed)
-x [package], --exclude=[package]
exclude package(s) by name or glob
--disableexcludes=[repo]
disable exclude from main, for a repo or for
everything
--obsoletes enable obsoletes processing during updates
--noplugins disable Yum plugins
--nogpgcheck disable gpg signature checking
--disableplugin=[plugin]
disable plugins by name
--enableplugin=[plugin]
enable plugins by name
--skip-broken skip packages with depsolving problems
--color=COLOR control whether color is used
--releasever=RELEASEVER
set value of $releasever in yum config and repo files
--setopt=SETOPTS set arbitrary config and repo options

Plugin Options:
------ Std Err -------
Failed

感谢 JtheRocker,终于让它工作了。这是工作代码,以防其他人遇到同样的问题。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;



public class test
{
public static void main(String[] args) throws IOException, InterruptedException
{
Runtime rt = Runtime.getRuntime();
String cmd[] = new String[] {"yum", "install", "-y", "rpm-build"};
Process ps = rt.exec(cmd);

BufferedReader readerStd = new BufferedReader(new InputStreamReader(ps.getInputStream()));

BufferedReader readerErr = new BufferedReader(new InputStreamReader(ps.getInputStream()));

String line = null;
while ((line = readerStd.readLine()) != null) {
System.out.println(line);
}
System.out.println("------ Std Err -------");
while ((line = readerErr.readLine()) != null) {
System.out.println(line);
}

if (ps.waitFor()==0) {
System.out.println("success");
} else {
System.out.println("Failed");
}
}
}

最佳答案

试试这个:

public class test 
{
public static void main(String[] args)
{
Runtime rt = Runtime.getRuntime();
String cmd[] = new String[] {"yum", "-y", "install", "rpm-build"};
Process ps = rt.exec(cmd);

BufferedReader readerStd = new BufferedReader(new InputStreamReader(ps.getInputStream()));

BufferedReader readerErr = new BufferedReader(new InputStreamReader(ps.getInputStream()));

String line = null;
while ((line = readerStd.readLine()) != null) {
System.out.println(line);
}
System.out.println("------ Std Err -------");
while ((line = readerErr.readLine()) != null) {
System.out.println(line);
}

if (ps.waitFor()==0) {
System.out.println("success");
} else {
System.out.println("Failed");
}
}
}

关于java - 如何从 jar 文件在 linux 命令行中执行 yum 安装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17533837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com