Java: execute command in a new cmd.exe as administrator - java

I want to execute a command from a Java-application using
Runtime.getRuntime.exec(command);
but the command need Admin-privileges. If I use
runas /user:Administrator "cmdName parameters"
nothing happens because I need to give user und pw as parameter to the command.
But I need to run a command to the cmd, so that a new cmd.exe starts as administrator and asks if I want to run cmd.exe as admin. After agree the command should be run in the admin-cmd. So like this:
String command = "popupNewCmdAsAdminAndRun "batWhichNeedsAdmin.bat" "
Runtime.getRuntime.exec(command);
Has anyone an Idea?
Thanks in advance!

you should do the following
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "command";
process.StartInfo.Verb = "runas";
process.Start();
process.WaitForExit(60000);

Start Command Prompt (CMD) from java as Administrator and Run command prompt as Administrator - or the silly way, try to pipe it:
pw | command

Research building projects and the executable jar file. Usually when you build a project, it gives you a commandline that can be used to execute the jar file in folder dist located in the projects folder. You can copy and paste the command line in cmd. But you could also use a bat file, or convert the executable jar file into an exe with the jar2exe converter. Not completely sure of the name because i am not posting on my computer.

Related

Command is executable in terminal that I opened, but cannot execute when I use java function to open terminal and execute it

I am trying to write a java program that can open terminal and input a command to it. The command is "wrspice" which can start an application. I have added "wrspice" PATH in .bashrc and it works fine. But when I write a java code using ProcessBuilder to execute, it pop up "bash: wrspice: command not found" error. Here is my java code:
final String[] wrappedCommand;
wrappedCommand = new String[]{ "xterm", "-hold", "-e", "wrspice"};
Process process = new
ProcessBuilder(wrappedCommand).redirectErrorStream(true).start();
This code works fine when I replace "wrspice" with "ls" to print the file list. But when I try "wrspice" it gives me error for "command not found".
Here is how I add "wrspice" in .bashrc:
export PATH=/usr/local/xictools/bin:$PATH
Anyone have any ideas? Thanks in advance.
Java isn't an interactive shell. Specify the full-path to your command instead. And you don't need new String[]. Like,
wrappedCommand = { "xterm", "-hold", "-e", "/usr/local/xictools/bin/wrspice"};
You should probably specify the path to xterm as well. Double check on your system.
wrappedCommand = { "/usr/bin/xterm", "-hold", "-e", "/usr/local/xictools/bin/wrspice"};

issue when running cmder.exe from a java application

I'm trying to run cmder.exe from a java application. I'm using the java ProcessBuilder class and it works great with cmd.exe, but never with cmder.exe.
This is my code:
command = "cd C:\\Users\\fxxx\\Documents\\plt 3.0\\git\\xxx-estatic";
ProcessBuilder builder = new ProcessBuilder("C:\\PLT 3.0\\abc\\cmder_mini\\cmder.exe", "/start", command);
I want to open the cmder command prompt and run a "cd" command. Cmder.exe is actually launched, but I get the "file name, directory name or volume label syntax is incorrect" error message and my command (cd...) is never executed.
Again, things work perfectly if I use cmd.exe.
Does someone know how to effectively run cmder.exe from a java application?
Thanks a lot.
RTFM!
Assuming that you use that cmder.exe, the doc states:
Cmder.exe Command Line Arguments
Argument Description
/C [user_root_path] Individual user Cmder root folder. Example: %userprofile%\cmder_config
/SINGLE Start Cmder is single mode.
/START [start_path] Folder path to start in.
/TASK [task_name] Task to start after launch.
That means that your code should be:
path = "cd C:\\Users\\fxxx\\Documents\\plt 3.0\\git\\xxx-estatic";
ProcessBuilder builder = new ProcessBuilder("C:\\PLT 3.0\\abc\\cmder_mini\\cmder.exe", "/start", path);

Failed to execute script in CMD

I have an .exe file on MyDocuments folder. I can't seem to run the program in the command prompt if I run C:\User\User\MyDocuments\Sample.exe. This gives me an error Failed to execute script.
But when I'll have the command prompt opened on the MyDocuments folder and only run Sample.exe, the programs runs perfectly.
What I want to do with this is I want to have a java program and execute Process p = Runtime.getRuntime().exec("C:\\User\\User\\MyDocuments\\Sample.exe"); and it will give me the Failed to execute script error.
Any I ideas what I missed?
As suggested by Application will not launch from command line full path, but will after CDing to directory I would make sure your executable does not depend on the current working directory.
Try Running:
Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
before your try and execute your executable.

how to run shell script in java using Cygwin

From a long time i am struggling for with this program. I am having a shell script which accepts parameters as version number and path for the files. then that script creates Zip file with the name of version number congaing all file files to the folder.
I have installed Cygwin on following path D:/cygwin. I am coping required files to same location where cygwin is installed D:\cygwin\bin
Command
D:/cygwin/bin/bash -c '/bin/test/app.sh 04.10 D:\cygwin\bin\ Test_files
Or Can any one please suggest how to run shell script in java using Cygwin.
Rewriting the Problem:-
When i am trying to run following Command in command prompt it gives error
sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
Error:-C:\Documents and Settings\sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
/usr/bin/app.sh: line 51: lib/lib.sh: No such file or directory
But if i run the same command at
D:cygwin\bin\Test>sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
It works fine. Can any one suggest me how to avoid this kind of errors.
Runtime run = Runtime.getRuntime();
Process p = run.exec("D:/cygwin/bin/bash -c \'/bin/test/app.sh 04.10 D:\cygwin\bin\ Test_files");
p.waitFor();

How to spawn a bash script from Java on Windows?

Is there a way to make Java spawn a bash script on Windows? I have cygwin installed, and I've associated the .sh file extension with cygwin bash. The following code, which works on Linux, isn't working:
String[] cmdArray = { "scriptName.sh", "-force", categoryName};
Process proc = Runtime.getRuntime().exec(cmdArray, null, directory);
Try adding either bash or start as the first parameter.
I think you'll have to provide the executable itself in order to do this. The Process.exec method only seems to execute exe files.

Categories