issue when running cmder.exe from a java application - java

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);

Related

Opening a PDF in java using Linux Terminal

I'm trying to open a PDF file in Linux with the xdg-open command in java.
String[] command = {"xdg-open","\""+path+"\""}
Process p = Runtime.getRuntime().exec(command,null);
p.waitFor();
When I run the code in terminal nothing happens even tho if I type it in terminal:
xdg-open path
it opens the PDF.
Any ideas whats wrong?
You should not escape the path: if the program was called, it was with an invalid path ("path" and not path).
String[] command = {"xdg-open", path}
The Runtime.getRuntime().exec(command,null); will use ProcessBuilder internally which, in the case of Linux, should invoke the system command execve.

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"};

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

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.

Change PWD of linux from JSP

I need to execute linux commands from JSP.
It is working fine.
But i need to start some sh file in a particular directory in linux through JSP. say /home/username/something/start.sh
try{
String command= "cd /home/username/something";
Runtime.getRuntime().exec(command);
Runtime.getRuntime().exec("./start.sh")
out.println("Child");
}
catch(Exception e)
{ out.println("Error");
}
It says FIle or Directory not found.
I tried Runtime.getRuntime().exec("pwd"), It is showing something like "java.lang.UNIXProcess#fc9d2b" !! :O
I need to change the pwd and execute some commands through jsp. How can i do that??
Any help would be appreciated.
You shouldn't (and actually, it seems you can't) set a working directory like that. Each Process object given by Runtime.exec() will have its own working directory.
As answered in How to use “cd” command using java runtime?, you should be using the three argument version of Runtime.exec(), in which you provide a File that will be the working directory. From its javadoc:
Executes the specified command and arguments in a separate process with the specified environment and working directory.
Or even better, use ProcessBuilder along with ProcessBuilder.directory() instead:
ProcessBuilder pb = new ProcessBuilder("start.sh");
pb.directory(new File("/home/username/something"));
Process p = pb.start();

How to run .reg file in Java

I need to install a .reg file (INTRANET) by using Java. How do i get my goal ?
Cheers,
You could use System.exec to launch regedit yourfile.reg
Here is how to do it :
String[] cmd = {"regedit", "yourfile.reg"};
Process p = Runtime.exec(cmd);
p.waitFor();
Last line is optional, it only allows you to wait until the operation is over.
If you're already on Java 1.6, just grab java.awt.Desktop:
Desktop.getDesktop().open(new File("c:/yourfile.reg"));
It will launch the file using the default application associated with it, as if you're doubleclicking the particular file in Windows explorer.
This can achieved through Process Builder in JAVA. Please consider the following example for this:
ProcessBuilder processBuilder = new ProcessBuilder("regedit", "reg_file_to_run.reg");
Process processToExecute = processBuilder.start();
And then you can optionally wait for the completion of process execution with this line:
processToExecute.waitFor();
Note: If command in your registry file asks for confirmation prompts while making changes in registry entries, you can perform it silently as well with '/s' option. Like this:
ProcessBuilder processBuilder = new ProcessBuilder("regedit", "/s", "reg_file_to_run.reg");
With this command would be executed silently without any confirmation prompt.

Categories