我正在尝试从主机操作系统(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);
处失败。我不断得到
block 引用>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/