How to read/open files with space in name - java

I'm trying to develop a server for mplayer using Java but I can't open files that have spaces in name (e.g. "File with space.mp3").
I'm following this tutorial here. The problem is, every time I try to open a file with spaces in name the getInputStream() read only the string before the space, generating a "file not found" error.
The path are correct in command, I tried even different formats (e.g. "File\ with\ space.mp3", "$PATH/File with space.mp3", etc), but nothing works.
What can I do to get data properly from getInputStream? How to avoid getInputStream to block when it founds a space in the String?
Ps. I use a linux system and the codes are the same as the link above (ctrl+c , ctrl+v).
thanks for the help.

The problem is the use of Runtime#exec. It thinks that the space in the file is another parameter.
Process mplayerProcess = Runtime.getRuntime().exec("/path/to/mplayer -slave -quiet -idle file/to/play.avi");
Instead, you should use ProcessBuilder which allows you to specify each parameter as a separate String eliminating the need to mess about with quotes.
ProcessBuilder pb = new ProcessBuilder("/path/to/mplayer", "-slave", "-quiet", "-idle", "file/to/play.avi");
// Other configuration options...
Process p = pb.start();

Related

How to enter UTF-8 characters inside a jar file using Scanner(System.in)

I have a java project in which i want to take input from the user.
I wrote the code in eclipse and it was running without any problems at all.
However, when I export my classes into an executable-jar file using eclipse and try to run it in the windows cmd, the Scanner(System.in) can't read charachters in UTF-8 (greek characters) or something else that i haven't thought about.
This is the part of the code where i run into the problem :
String yesORno = inp.stringScanner(); // basically a nextLine()
while (!(yesORno.equals("ΝΑΙ") || yesORno.equals("ΟΧΙ"))) { // ΝΑΙ and OXI are greek characters not latin
System.out.println("Παρακαλώ πληκτρολογίστε 'ΝΑΙ' ή 'ΟΧΙ'"); // please type ΝΑΙ or ΟΧΙ in greek
yesORno = inp.stringScanner(); // take input again
}
inp is an object of an other class which i use to take inputs, in this case with the method stringScanner()
public String stringScanner() {
Scanner in = new Scanner(System.in);
return in.nextLine();
}
So when i run the code in eclipse and enter some sample characters for testing i get :
And that's what i want to happen every time.
But when i run the jar file i get :
As you can see the jar file for some reason doesn't recognise greek NAI and yesORno.equals("ΝΑΙ") doesn't return true to stop the while loop.
The same happens with OXI
I have tried running the jar file by using a .bat file like :
start java -Dfile.encoding=UTF-8 -jar Myfile.jar
but no solution.
I've done a lot of reserch to resolve this problem but I have found nothing.
I would appreciate your help
The JVM argument -Dfile.encoding tells the JVM what is the default encoding for (text) files it may encounter. This includes stdin, stdout and stderr – mapped to System.in, System.out and System.err. But the argument will not change anything in the operating system.
Most probably, your Windows CMD is using the Windows-1253 encoding, not UTF-8. When you tell the JVM with the -Dfile.encoding argument that it would be UTF-8, that would be an outright lie …
Try start java -Dfile.encoding=Windows-1253 -jar Myfile.jar or start java -Dfile.encoding=ISO-8859-7 -jar Myfile.jar.
If you setup your system with Windows-1253, the second option may cause other problems, as ISO-8859-7 and Windows-1253 are not fully compatible. But for your test it should do the job.
According to the documentation, the way you use the scanner will always depend on the operating system's encoding settings.
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Scanner.html#%3Cinit%3E(java.io.InputStream)
Look at the alternative constructors - you can define the encoding there directly. Your code could look like
Scanner in = new Scanner(System.in, "UTF-8");

Java ProcessBuilder Error When Space(s) In Path

So I spent half a day trying to get this to work with no positive result. I am using a Java ProcessBuilder to execute some .exe file with a couple of arguments, but the file-path contain space(s) and somehow I can't get it to work properly. I have checked a number of other SO posts and implemented solutions like surround code with escaped quotes and splitting it up in command and arguments etc. My code is below:
try {
ProcessBuilder pBuilder = new ProcessBuilder(
// Main Command.
"C:\\namewith space\\database\\postgres_db\\bin\\pg_ctl.exe",
// Command Parameters.
"start",
"-D C:\\namewith space\\database\\database",
/*The quotes in the next argument are necessary, the -o stands for 'options' and everything between the quotes are the actual database parameters which to start the Database with.*/
"-o \"-p 15000\"",
"-l C:\\namewith space\\database\\postgres_db\\bin\\postgres_log.txt");
File log = new File("\"C:\\folder\\log.txt\"");
pBuilder.redirectErrorStream(true);
pBuilder.redirectOutput(Redirect.appendTo(log));
Process p = pBuilder.start();
} catch (IOException ex) {
System.out.println("Exception Occurred: " + ex);
}
I have tried so far:
Surrounding and not surrounding each/any of the paths in the above code with escaped quotes just in case that matters (something tells me it does...).
Using the Runtime.getRuntime().exec("full command with/without any/all escaped quotes"); method, but when searching on SO I found out everyone is saying you should use the ProcessBuilder instead.
Adding parts of the above code together in different ways in the ProcessBuiler's first command String, like "\"C:\\namewith space\\database\\postgres_db\\bin\\pg_ctl.exe\" start"
The files are 100% located at the given paths, I checked this by hand and by pasting the paths in the File Explorer over 10 times.
Splitting up the command into setting the working directory of the command to C:\namewith space\ and then adding the args without that part.
The error (via the System.out.println("Exception Occurred: " + ex); ) I keep getting is: java.io.IOException: Cannot run program "C:\namewith space\database\postgres_db\bin\pg_ctl.exe": The filename, directory name, or volume label syntax is incorrect.
Please let me know if you need any extra parts/code and I will do my best to provide it in detail.
Try to break the problem down.
First just read the absolute path shown in the IOException into a File object and call the exists() method to check that the file really exists and the JVM has access to it.
If that didn't work, fix your path or the access permissions. If the file really exists and you can access it then create the ProcessBuilder without any parameters, just with absolute path to your exe.
You shouldn't get the IOException now. Then add the parameters one by one. If you find one that breaks the thing, then fix the parameter (maybe the double quotes are missing) and go onto the next until you finish.

Run batch File using Java (space in arguements issue)

I am trying to run a batch file from Java. The following works:
Process p1 = java.lang.Runtime.getRuntime().exec("D:\\Users\\xx\\Documents\\NetBeansProjects\\Test1\\New folder\\batch.bat");
The following does not work:
Process p1 = java.lang.Runtime.getRuntime().exec("D:\\Users\\xx\\Desktop\\Jar Test\\New folder\\batch.bat");
The error I get is "Windows cannot find 'D:\Users\xx\Desktop\Jar'. Make sure you typed the name correctly, and then try again."
Although there are spaces in both paths, for some reason the second one is not running. I have read numerous threads about escaping spaces, but none of the solutions there worked with me.
Note: I have tried to use process builder but I am also facing the same issue.
Try
Process p1 = Runtime.getRuntime().exec(new String[] {"D:\\Users\\xx\\Documents\\NetBeansProjects\\Test1\\New folder\\batch.bat"});
This form of exec() wont tokenize your input by spaces for you.

Runtime.exec(String) limiting String

I'm trying to use the Java function Runetime.exec(String) to run a program in the startup folder of a windows 7 computer like so:
Runtime.getRuntime().exec(runner.getPath() + "\\run.bat");
And when I run this I get an error saying the command cannot be run:
Exception in thread "main" java.io.IOException: Cannot run program ""C:\Users\ly
ndsey\AppData\Roaming\Microsoft\Windows\Start": CreateProcess error=2, The syste
m cannot find the file specified
As you can see, the file name is cut off at the "\Windows\Start" when it should continue to "\Windows\Startup\run.bat".. Is there an alternative I can use?
Considering runner as a File instance, this should work.
Desktop.getDesktop().open(new File(runner, "run.bat"));
It uses Desktop class instead of Runtime, so you don't have to convert your File (runner) to its String representation (which is error prone). Runner is now used 'as is' as the parent directory of the "run.bat" you want to execute.
Other advantage of Desktop class : you can now open any file you want.
As an alternative you can use ProcessBuilder. I feel ProcessBuilder is more safe than Runtime.getRuntime().exec http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
String[] command = {"CMD", "/C", "dir"};
ProcessBuilder pb = new ProcessBuilder( command );
//set up your work directory if needed
pb.directory(new File("c:\\path"));
Process process = pb.start();
as i can see from the error you give, and i hope it's a copy past, you string runner.getPath() for some reason start and end with "\"" which make the whole path invalid. check that and remove it if needed
if you have the file already and you just need it's path you can use
runner.getAbsolutePath()
also, if runner is a file, getPath will give you the file path including the path, so your code will surely won't work. instead use:
String path = runner.getPath();
path = path.substring(0, path.lastIndexOf("\\")) + "\\run.bat";
Runtime.getRuntime().exec(path);
You should avoid the exec(String) method, which attempts to parse the entire string into command + arguments. The safe option is exec(String[]), which presupposes the first array element is the command and the rest are arguments.
So, writing
Runtime.getRuntime.exec(new String[] { yourCommandString })
is a surefire way of getting the right message across.

Set java.security.policy property to a cross-platform file path

I'm trying to set the system property "java.security.policy" programmatically.
It works, as long as the path to the security policy file has no spaces.
File myFileReference = new File("C:\folder_name\security.policy")
System.setProperty("java.security.policy", myFileReference.getAbsolutePath());
System.setSecurityManager(new RMISecurityManager());
If there are spaces int the file path, they get escaped with a %20, like this
"C:\folder%20name\security.policy".
The code above executes fine, but then all security checks fail. I assume setProperty doesn't really find the file.
On Windows, writing the file name without that escaping for spaces works.
System.setProperty("java.security.policy", "C:\\some folder\\wideopen.policy");
So, the problem seems to be that %20 space escaping. I could replace it using a regex, but maybe that would make it work just on Windows, and fails somewhere else.
Also, I don't want to hard-code the file path like that.
I looked at the Java doc for a File function that returns a "System.setProperty" compatible path name that works on any platform. I also tried things like toURI().toString(), to no avail.
Is there an elegant way to get a working file path String from a File reference in 1 line of code?
EDIT:
This was simplified code, I construct the file like this
URL policyURL = Class.class.getResource("/sub local folder/wideopen.policy");
new File(policyURL.getFile())
I needed a relative path, so I used that little getResource trick, which happens to return an URL, with the nasty %20 escapings.
I can use an URLDecoder to strip them away now that I know what the problem is.
But is there a less error prone way?
I solved using the URLDecoder class. Here is a complete example of what I was trying to do, and the solution that made it work.
//here is the trick
URL policyFileURL = Class.class.getResource("/server/model/easy.policy");
String policyFilePath = "";
try {
policyFilePath = URLDecoder.decode(policyFileURL.getFile(), "UTF-8");
}
catch (UnsupportedEncodingException e) {}
//here what I wanted to do (now it works)
server.activate(port, new File(policyFilePath));

Categories

Resources