I have Java Code that launch process of WinSCP tool and connects to a Unix machine and then call a xxxx.exe located on the Unix machine.
The problem is that xxxx.exe accepts a parameter of a type File. So I need to upload this to the remote machine and then passed to the xxxx.exe.... that is failing
and I'm trying to avoid the temporary folders as possible.
small Code
Process p = Runtime.getRuntime().exec("rTool\\WinSCP.com /script=folder\\code.txt < C:\\FILESTOUPLOADS\\upload1.txt" );
The login information goes in code.txt as supported by WinSCP.com
file redirection (i.e. the "<" symbol) is handled my the command processor, which Runtime.exec() does not use. As mentioned in comments already, first use the String[] version of exec so that you don't have issues with command parsing. second, you need to invoke the command processor to handle the file redirection (e.g. using "cmd.exe /k"), or handle it yourself in java.
Why not use ProcessBuilder to change working directory and set path of the file from that directory
public ProcessBuilder directory(File directory)Sets this process builder's working directory. Subprocesses subsequently started by this object's start() method will use this as their working directory. The argument may be null -- this means to use the working directory of the current Java process, usually the directory named by the system property user.dir, as the working directory of the child process.
Parameters:
directory - The new working directory
Returns:
This process builder
Related
I am trying to work out the semantics of using Java ProcessBuilder to call operating system processes and read this line out of the javadocs for the start command:
"This method checks that the command is a valid operating system command. Which commands are valid is system-dependent, but at the very least the command must be a non-empty list of non-null strings."
Tell me, What is considered a valid process for Mac and for Windows? Is it anything that can be found on the PATH variable?
Is it anything that can be found on the PATH variable?
Yes it is; although you can also specify the full path of the command if you like (such as "/bin/ls"). Another test if of course to check if the file in question is a regular file and has execution permissions.
Note: this will launch a "real" process, it will not launch it via a command interpreter; as such don't attempt to use pipes, file globs, shell builtins etc: those are interpreted by sh/cmd.
I need to create a directory on unix machine. I think the below code will work fine on unix machine but fails while testing it on local windows machine. Where does this directory get created on my local machine ?
String xmlDir = "/home/data/logs"
File xmlDirectory = new File(xmlDir);
xmlDirectory.mkdir();
I tried below directory path and it worked fine on windows machine. But i had to use the mkdirs() instead of mkdir() method which needs to be used for unix directory creation?
String xmlDir = "C:\\home\\data\\logs"
File xmlDirectory = new File(xmlDir);
xmlDirectory.mkdirs();
How can I make it work locally as well as n unix machine ? Is there a better way for File and Directory creation ?
--Thanks--
You should use the System user.home property which will return the user's home directory in a system independent manner, for example...
File home = new File(System.getProperty("user.home"));
mkdir will only create the last element in the path, where as mkdirs will create all the elements that do not exist. Using mkdirs is probably a slightly better idea as it ensures (where permissions allow) that all elements in the path will be created if they do not exist
You have already hit on the answer: Just use mkdirs(). It is not platform dependent. However, if you include platform dependent nomenclature, then you'll run into trouble when moving the code from one environment to another. Just be sure to use platform independent code, or at the very least, check for the OS before doing so via System.getProperty("os.name");
I am running a server on my machine. When Servlet receives a message, the corresponding Visual C++ ".exe" need to start running.
I am using following code to start the exe. But I am getting "Microsoft Visual C++ Debug Error". The code is as follows:-
if(strLine.equals(location))//same place do not do anything
{
Runtime rt=Runtime.getRuntime();
String cmd[]={"cmd.exe", "/c", "C:\\Users\\nabeel.OUCS1289\\Documents\\Visual Studio 2010\\Projects\\Scene Localization - (FM)\\Debug\\Scene Localization.exe"};
rt.exec(cmd);
System.out.println("Same place so dont do anyuthing");
}
Please help me out in this regard.
The EXE file to be executed is located in a user profile directory. Does the account running the JRE/Webserver does have read & execute permissions on that particular directory?
Furthermore remove the indirect execution via cmd.exe /c .... This is total unnecessary for regular executables. It is only required in case you are executing a command that is provided by cmd.exe itself and therefore can not be executed via an exe file.
I am trying to run a perl command with Java runtime exec in linux/ubuntu/gnome. The command generates an pdf file, but it saves it in my home folder. Is there any way that the exec method can set an output path for the commands executed? Thanks in advance.
The exec method just runs the command on the operating system, so you'll want to change the command you're running with exec more than anything in "Java" per se.
There are a few possibilities:
Change the working directory of your java program. The .pdf is being saved in your working directory because this is where the program is being run.
Unfortunately it's not simple to change this value after the program has been launched. It is, however, trivial to change before the program starts; just change the working directory before starting the program.
Move the file to it's desired location after it's been created in your home directory.
Change the command so that it includes the target location. Your perl script may have an option that will enable you to save it's output to a certain location (usually -o or --output). Using this your program would change from:
Runtime.exec("perl someprogram");
to something like:
Runtime.exec("perl someprogram -o /path/to/some.file")
You might be able to use "output redirection", if there is no option to do this.
Try something like what's below as your argument:
Runtime.exec("perl someprogram > /path/to/some.file")
Unfortunately, without knowing more details of your situation I can't provide more concrete advice.
While each approach has benefits and drawbacks, it's probably best to just implement the one that you understand best; if you can't get one to work, try another.
A good, free online resource for learning is Introduction to Linux: A Hands On Guide.
Section 2.2 has details on cd which you can use for 1..
Section 3.3, section 3 teaches about the mv command, which will be useful in 2..
Section 5.1 is about I/O redirection. Knowing about "output redirection" and the > operator, are important for 4..
For 3., you'll have to consult the documentation of the perl program you're using.
You could modify the Perl script to accept an absolute path for the output.
You can trying setting the working directory using exec(java.lang.String[], java.lang.String[], java.io.File) where File is the directory the command is executed from.
If all else fails, you'll can always copy the generated file from the Home directory to your final location.
I am invoking the command using a bash shell in Linux. This command completes its operation using a Java class. I want to retrieve the present working directory from where the command is invoked. When I am using System.getProperty("user.dir") I am getting the path from where the Java class is invoked. Can anyone help in this.
You will have to pass the directory in from the shell to the JVM on the command line.
new File(".")
You can then call getAbsolutePath or whatever you like.