0

Например, что бы выпонлить команду dir через cmd, и оставить консоль открытой нужно написать так:

    Runtime.getRuntime().exec("cmd /c start cmd /k dir");

А как сделать аналогичное через powershell? Пробовал следующее и многое другое, безуспешно:

    Runtime.getRuntime().exec("powershell /c start cmd /k dir"); // нет
    Runtime.getRuntime().exec("powershell start powershell dir"); // команду выполняет, но закрывает окно, а как параметр добавить неопнятно
    Runtime.getRuntime().exec("powershell start powershell -noexit dir"); // нет
    Runtime.getRuntime().exec("powershell start powershell -noexit -command dir"); // нет
    Runtime.getRuntime().exec("powershell -command start powershell -noexit -command dir"); // нет
    Runtime.getRuntime().exec("powershell start powershell \"-noexit\", \"-command {dir}\""); // нет
    Runtime.getRuntime().exec("powershell start powershell \"-noexit\", \"-command {dir}\""); // нет
    Runtime.getRuntime().exec("powershell \"-command start powershell '-noexit', '-command dir'\""); // нет
    Runtime.getRuntime().exec("powershell -command {start powershell \"-noexit\", \"-command dir\"}"); // нет

1 ответ 1

0

Используя синтаксис скриптового языка powershell. Из-за его богатых возможностей, полагаю, если написать так же просто, как для cmd – может быть не ясно что именно нужно сделать.

Runtime.getRuntime().exec("powershell -command \"& start powershell '-noexit', '-command dir'\"");

// powershell -command "& start powershell '-noexit', '-command dir'"

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.