running external exe with spaces in java - java

I have been trying for 4 hours now to get this thing to run and I managed to do it without understanding why :/
I have created a very simple java program with a GUI containing 2 textboxes where people can type the path to an exe-file.
When a button is clicked it reads the text in this box and runs the corresponding software.
This seems to work when people type ""C:\Program Files (x86)\thatsoftware\" in the directory box and "C:\Program Files (x86)\thatsoftware\run this.exe -arg" in the file to run box:
Runtime.getRuntime().exec(txtFile.getText().toString(), null, new File(txtPath.getText().toString()));
However, when I set only 1 directory box and append (hardcoded) the file and argument to it, it will not work:
String fileToRun=txtPath.getText().toString()+"run this.exe -arg";
Runtime.getRuntime().exec(fileToRun, null, new File(txtPath.getText().toString()));
I have tried passing the file as an array as well:
String fileToRun[]={txtPath.getText().toString(),"run this.exe"," -arg"};
Runtime.getRuntime().exec(fileToRun, null, new File(txtPath.getText().toString()));
to no avail.
The same kind of problems pop up when I try to run it as a processbuilder.
I will get an error message like "file C:\Program Files (x86)\thatsoftware\ -arg" does not exist." Very weird, since the argument is passed, but not the file name apparently.
I can manage to run it when the whole string is typed in the text box by the user, but not if I add the argument and or filename to it in the code.
Could anyone be so kind to explain this to me and tell how it can be done with only one text box?

As I know you must put each element as separate field:
String fileToRun[]={
txtPath.getText().toString(),
"run",
"this.exe",
" -arg"
};
I don't know if you use Swing or not but Swing has javax.swing.JFileChooser.
//config fileChooser
JFileChooser fc = new JFileChooser(lastOpenDir);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setDialogTitle("Load Beacon List");
fc.removeChoosableFileFilter(fc.getFileFilter()); //remove the default file filter
FileFilter filter = new FileNameExtensionFilter("EXE file", "exe");
fc.addChoosableFileFilter(filter); //add XML file filter
//show dialog
int returnVal = fc.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File selectedDir = fc.getSelectedFile();
...

You need to wrap the executable in escaped quotes \" like this:
Runtime runtime = Runtime.getRuntime();
Process ps = runtime.exec("\"run this.exe\"");
Or using a path and the argument as you need:
Process ps = runtime.exec("\"C:\\Program Files (x86)\\Thatsoftware\\my exe.bat\" -arg");

boy do i feel like an idiot...
The java errors threw me off, but the problem was a missing slash before "run this.exe".
so many shames...

Related

In Java, how do I find the proper path to open a txt file that's name is provided as a command line arguement?

I'm working on a program that is supposed to take two files as command line arguments, open the files, and read data from the files to make a data structure.
So far, I have been able to make the structure using File() to open the files and Scanner to read the data. The problem is that I have been providing a specific path to the call for File like this
File f1 = new File("F:/MinSpan/resources/cities.txt");
Scanner sc1 = new Scanner(f1);
I don't think this is going to work for the person who tries to run this program, because I have provided the path for where my specific txt files are located - they're on my flash drive (F) and in some folders. Is there a way I can program this to pass some kind of args[] value in for File() based on the cmd arguement the user has provided?
I have already tried just doing new File(args[2]) , and it can't find the file because there is no path.
The reason for that is because, if you are passing in only two paths, args[2] wont return anything, because args[] starts at 0. So you'd want to use:
new File(args[0]);
new File(args[1]);
Does that make sense?
If you're going for something like java -jar program.jar FILE, then have the program check for the String in args[] at index 0.
Then, construct your file. Check if the file exists (in java.io, it's File.exists()) and return an error message to the user if it's wrong.

Understanding JFileChooser behaviour

I am trying to open a JFileChooser dialog to let the user decide his wish-directory for following operations.
Following is my current code:
JFileChooser chooser;
if(pref.get("LAST_PATH", "") != null){
chooser = new JFileChooser(pref.get("LAST_PATH", ""));
} else{
chooser = new JFileChooser(home_dir);
}
//chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
int retVal = chooser.showOpenDialog(frame);
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory().toString());
home_dir is a static String pointing to the users Download-directory.
The behaviour I do not understand:
home_dir = C:/Users/Scy/Downloads
Press OK without selecting any file(Or directory)
Output: C:/Users/Scy
home_dir = C:/Users/Scy/Downloads
Select any file within Downloads
Output: C:/Users/Scy/Downloads
Why do I not get the full-path(C:/Users/Scy/Downloads) as output when not selecting anything and just pressing OK? (With DIRECTORIES_ONLY activated, can't press OK without selecting anything without DIRECTORIES_ONLY)
Edit: I just noticed that when I just press the Cancel button without selecting anything the output is indeed what I expect, C:/Users/Scy/Downloads.
Based on an answer on this post I tried the following:
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(home_dir)); //home_dir = "C:/Users/Scy/Downloads"
The result was exactly the same as above. Pressing the Cancel button results in the full path output, while pressing OK/Accept results in C:/Users/Scy.
Maybe the 'selected file'(or directory) is in the 'current directory' (which you are retrieving atm.)?
If you want the current selected file, chooser.getSelectedFile() is what you are looking for. Keep in mind that when switching to DirectoryOnly mode this method will return a directory (e.g. a File instance representing a directory).
The method chooser.getCurrentDirectory() will return the parent directory of the current selected file which explains the unexpected results. (getSelectedFile.getParentFile() will most likely return the same file)
If you are trying to retrieve the parentDirectory, you set the starting directory incorrect. Notice how you pass in the first constructor a selected File? This means in the second constructor the 'home_dir' will be the selected File. If you only want to set 'home_dir' as starting directory, you should use the no-args constructor and call chooser.setCurrentDirector(new File(home_dir)) instead. Here is a snippit of what your code could look like:
JFileChooser chooser;
if(pref.get("LAST_PATH", "") != null){
// set last SELECTED file/directory path.
chooser = new JFileChooser(pref.get("LAST_PATH", ""));
} else{
// set currentDirectory, but dont select anything yet.
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(home_dir));
}
getCurrentDirectory() returns a directory name, not a file name. If the user selects a file, this method returns the name of the directory which contains that file. If you want the name of the file which was selected, you should use getSelectedFile(). If you haven't yet, you should read this Oracle tutorial on file choosers.

How do you set up command line args in Eclipse for the MAC? (Intent is to submit to online programming challenges)

I know that you must add command line arguments into the "Run Configurations" in Eclipse to get your command line arguments to be passed every time by default. This worked fine on my PC.
The purpose of this question is to create a simple program that can be submitted to an online programming challenge site (like codeeval). The system provides a file path to the command line args[0] and then you manipulate the file and its data.
On the PC I had my Class folder > Default Package > (file.txt and TestCode.java)
The project was set up with a run configuration with simply file.txt in the program arguments section.
On the MAC this doesn't seem to be working. I get a fileNotFoundExcepion. I'm new to MAC so I'm thinking this might be a problem with file extensions not being what I think they are. I saved a file as "file.txt" but if I save it as "file" MAC doesn't show the file extensions and I'm not sure if MAC supports .txt by default.
If it doesn't support .txt, what file type is a "text document"? I tried saving the text document as "file" leaving off any extension, and then adding file.rtf or file.txt or even file to the Program arguments and none of that works. It all gives me a fileNotFoundException.
EDIT
The intent is to be able to develop solutions to the CodeEval (or similar) website and submit them. I have previously solved many problems on CodeEval and turned them in with the code below from a PC. This, however, doesn't work on MAC. The answer involving the use of the URL does not work when run from the solution checking platform (presumably because the program is not actually saved onto the system).
EDIT 2
My entire program:
public class TestCode {
public static void main (String[] args)throws IOException{
File filename = new File(args[0]);
Scanner file = new Scanner(filename); // returns the fileNotFoundException
while( file.hasNextLine()){
String line = file.nextLine();
System.out.println(line);
}
}
}
Under "Run Configurations" in the Arguments tab > Program Arguments I have tried putting file, file.txt, file.rtf all three with a "text document" in the same directory as the above program. I have tried naming that file file, file.txt and file.rtf And i tried every combination of these names.
Did you try to use the absolute file name as command line parameter? This should be something like /Users/name/path/to/your/file. If the file is part of your project, you can also use a variable such as ${project_loc}/file (try the button Variables… below the Program Arguments field.
Replace your code with this
URL resource = TestCode.class.getResource(args[0]);
Scanner file = new Scanner(new File(resource.getFile()).getAbsoluteFile());

JFileChooser change default directory in Windows

I want to change the default directory of my JFileChooser to "My Music" on Windows.
This directory is C:\Users\Fre\Music on my account because my username is Fre
The default is set on C:\Users\Fre\Documents (depends on OS i think).
How can I change this?
You can use the API method setCurrentDirectory when initializing your JFileChooser objects:
public void setCurrentDirectory(File dir)
Sample usage might be like:
yourFileChooser.setCurrentDirectory(new File
(System.getProperty("user.home") + System.getProperty("file.separator")+ "Music"));
why don't you just give the FileChooser the path when you create it, like:
JFileChooser chooser = new JFileChooser("C:\\Users\\Fre\\Music\\");
Sorry for taking your time,
Just found the answer myself:
String userhome = System.getProperty("user.home");
JFileChooser fc = new JFileChooser(userhome +"\\Music");
JFileChooser openFile = new JFileChooser("C:\\Users\\Fre\\Music");
Creating all your own code, so as to set a default file directory is unnecessary and lengthy. A much easier and quicker way of doing it is by just right clicking on the File Chooser itself on Design view and right clicking 'customise code'.
Customise Code for File Chooser
This will show you the vital code for that GUI component. From the drop down box next to the top line of code, select 'custom creation'.
This will allow you to customise what fileChooser = is assigned to. Between the curly brackets JFileChooser() you can either hard code in the file directory with speech marks like this.
JFileChooser("C:\Users\user\Documents")
or type in a name that for a variable you created earlier. This variable would hold the file directory. I would recommend the latter option, though either will work fine.
Hope this helps.
p.s. sorry about having to use a link for the photo. I don't have enough privilege yet.
You can Change the default directory of my JFileChooser to "Directory you want" on Windows
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("put here your directory"));
int result = fileChooser.showOpenDialog(getParent());
if (result == JFileChooser.APPROVE_OPTION)
{
File selectedFile = fileChooser.getSelectedFile();
jTextField.setText(selectedFile.getAbsolutePath());
}
Pretty Simple:
JFileChooser browseImageFile = new JFileChooser("User Defined Directory");

How to get proper path in Java using JFileChooser as per the Operating system

In my Java application I need to select a path using JFileChooser. The code that I have written is as follows:
jfChooser = new JFileChooser();
jfChooser.setCurrentDirectory(new java.io.File("."));
jfChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (jfChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): "+ jfChooser.getCurrentDirectory());
System.out.println("getSelectedFile() : "+ jfChooser.getSelectedFile());
tfPath.setText(jfChooser.getSelectedFile().getAbsolutePath()); // the selected path set to textfield which is lated get by the program
}
else {
System.out.println("No Selection ");
}
I am getting the path properly.For example, here I am getting the path (In Windows os)
String choosedPath=tfPath.getText().trimm();
Now actually I want to create a another directory on a given path (i.e. inside newfolder directory) programatically.
For that I have new directory name "newdir" so the string passed to File constructor for creating this directory is as follows:
File createFolder = new File("choosedPath"+"\\"+"newdir");
Now the problem is that my application may run on windows or may run on Linux so according to that the filepath seperator varies (i.e. '/' for windows and '\' for linux)
How do I Overcome this problem so that I will get propper slashes in path according to OS?
new File(choosedPath, "newDir");
Platform dependent file separator gonna be choose automatically.
You can too use File.separator to get platform dependent separator to construct the string but you going to end with more code than first solution.
Use File.separator instead of / or \.

Categories