java - VirtualBox Sdk Java - 从主机操作系统在 guest 操作系统内执行命令

标签 java virtualbox

我正在尝试从主机操作系统(Ubuntu 14.04)在 guest 操作系统(Ubuntu 服务器)内创建用户。

这是我的代码

IGuestProcess process = null;

ISession session = manager.getSessionObject(); //VirtualBoxManager

machine.lockMachine(session, LockType.Shared); //IMachine

IConsole console = session.getConsole();

IGuest guest = console.getGuest();

IGuestSession guestSession = guest.createSession("registerdUserWithRights", "hisPasswd", "", "");

Long time = 100000L;

GuestSessionWaitResult result = guestSession.waitFor(time, time);       

if(result == GuestSessionWaitResult.Start)
    process = guestSession.processCreate("adduser --disabled-password --gecos \"\" username", null, null, null, 0L);

ProcessWaitResult waitResult = process.waitFor(1L, time);

if(waitResult == ProcessWaitResult.Start)
    System.out.println("started");

但是这在 ProcessWaitResult waitResult = process.waitFor(1L, time); 处失败。我不断得到

VBox error: VirtualBox error: rc=0x80bb0005 The specified file was not found on guest (0x80bb0005)

我哪里出错了?

最佳答案

我解决了。诀窍是发出命令的整个路径。我使用which <command>得到了它。另外,我必须在单独的数组列表中传递命令的参数。

IGuestProcess process = null;

ISession session = manager.getSessionObject(); //VirtualBoxManager

machine.lockMachine(session, LockType.Shared); //IMachine

IConsole console = session.getConsole();

IGuest guest = console.getGuest();

IGuestSession guestSession = guest.createSession("registerdUserWithRights", "hisPasswd", "", "");

Long time = 100000L;

GuestSessionWaitResult result = guestSession.waitFor(time, time);       

List<String> argumentsForProcess = Arrays.asList("--disabled-password", "--gecos", "'" + name + "'", username);

if(result == GuestSessionWaitResult.Start)
    createUser = guestSession.processCreate("/usr/sbin/adduser", argumentsForProcess, null, null, 0L);


ProcessWaitResult waitResult = process.waitFor(1L, time);

if(waitResult == ProcessWaitResult.Start)
    System.out.println("started");
guestSession.close();
session.unlockMachine();

关于java - VirtualBox Sdk Java - 从主机操作系统在 guest 操作系统内执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28280777/

相关文章:

matlab - VirtualBox 中主机和 guest 之间通过 COM 端口连接

virtualbox - Vt-x/AMD-V 硬件加速在您的系统上不可用

java - Eclipse环境变量错误

java - 为什么 firebase 用户 token 会发生变化?

docker 与共享文件夹 d 驱动器

android - 如何让菜单键在 Mac 上的 VirtualBox/Android x86 上工作?

java - 我以为我已经准备好运行该程序,但是当我运行它时什么也没有发生

java - Z3 JAVA-API 中设置超时的错误

java - 从 URL 获取文件名

virtualbox - 甲骨文 VirtualBox "VT-x is disabled in the BIOS"