I want to run something similar to the following:
java MyProgram C:\Path\To\My\File
When I do this and output the contents of the first argument, it outputs:
C:PathToMyFile
This works, however:
java MyProgram "C:\Path\To\My\File"
But I want to be able to do the first command instead of the second. How can I achieve this?
The \ charecter is used to "escape" special characters. This means it tells the program to ignore them, or not do anything special to them. To make this work just use \\ instead of \. This escapes the \. So use
C:\\Path\\To\\My\\File
Related
I want some code in my program to run only if the user has input the character '*' at the command-line as a command-line argument. This is the code I've used:-
//myfile.java
import java.io.*;
public class myfile {
public static void main(String[] args) {
if(args[0].equals("*")){
//do stuff
System.out.println(args[0]);//added this line to see what exactly was being passed
}
}
}
When this program is executed at the command-line by entering:-
java myfile *
the output I'm expecting to see on the screen is the asterisk character, instead the output displayed is 'myfile.class'. Where am I going wrong? Why does Java change the asterisk to the .class file?
Also, note that the program worked perfectly the first four times I executed it and then started doing this!
Thanks in advance for your help.
Where am I going wrong?
The star character needs to be quoted or escaped. Run your java program like this:
java myfile "*"
or
java myfile \*
Why does Java change the asterisk to the .class file?
It doesn't. It is your shell that is doing it. It is shell file expansion ... or "globbing" as it is also called.
Run "ls *" or "echo *" and you will see that the same thing happens.
The command terminal already replaces the asterisk and java already gets the value that you see. I'd use any other character, that has no special meaning to the command terminal or otherwise you must escape the asterisk in your command.
Actually escaping arguments on Windows and especially in cmd.exe is non-trivial. This nice article explains it in detail: Everyone quotes command line arguments the wrong way :
the takaway for your case is: surround the asterisk with quotes.
Answer to your question in the comment:
Using the escape character worked! But I still don't get why it worked without the escape character the first few times
I am not sure, but maybe you run into this behavior: It makes a difference if the pattern can be expanded or not. For example, when I pass Test* as argument, then there are 2 cases to consider:
in the current folder there is a file called Test1.txt: then your java program will get Test1.txt as argument
when there are no matching files, your program will get Test* as argument
However, I am not sure, how this would apply to your case, since you only pass *: that should only work in an empty directory.
The return value of a value from getPath() from the File Class is something like this
"C:\Users\Daniel\Desktop\ASDF.mp3".
To use the Desktop class from java to play a file, the path would have to be fed into a file, with a path similar to
"C:\\Users\\Daniel\\Desktop\\ASDF.mp3"
Since the \ is a reserved character(From my understanding) to make a new file you must use a double backslash to dictate that it is a file. My problem is that when I try to get the path I need to transform it into a double slash version. The .replaceAll() method doesn't allow for '\' since it's a reserved character but the .replace() method does.
To work around this would I just have to loop through to find all instances and replace them one at a time? Or is there a simpler work around? Also I would like to know if I am receiving this error due to it being a reserved character, or if I am completely wrong.
The two strings above are actually exactly the same.
When Java or other language outputs a string it only displays one slash '\', but when you are typing the string into double quotes you need double backslash '\\' so the Java parser knows it's one slash. Backslash is used for many other escape characters, so this is only way parser will know.
(Even when typing this answer, I needed 4 backslashes to make only 2!
One of the arguments passed to my java program is like this, ab|cd. Initially, when I run the java program, like this,
$ java className ab|cd
it fails, since | is interpreted in the linux shell as a pipe symbol. So only ab is passed into java program. So I made a second attempt:
$ java className "ab|cd"
This time, "ab|cd" is passed in, but the value includes the double quotes. What the program is really intended to have is ab|cd. How can I pass in the correct value without the quotes?
In the command shell, you can escape out characters using the '\' character.
java className ab\|cd
Try (for Linux):
$ java className ab\|cd
For Windows:
java className ab^|cd
Try this,
"\" is used inorder to nullify the effect of the characters which have special meanings.
java className ab\|cd
Maybe try this:
arg="ab|cd"
java className $arg
I run egrep using Java Runtime.exec()
String command = "egrep \'(Success|Loading\\.\\.\\.|Loaded : READY|Found a running instance)\' "+ instance.getPath() + "/log";
Runtime.getRuntime().exec(command);
The stdout is always null and stderr shows "egrep: Unmatched ( or (". but when I copy the command to shell and run, it returns the correct value.
The solution is pretty simple: (Success|Loading\\.\\.\\.|Loaded is not a valid regex.
You can't protect white space with quotes when using Process.exec(String). Always use the versions of exec() that take an array or, even better, use ProcessBuilder.
That way, you can pass each argument as a single Java String and spaces and other special characters won't create any problems.
The single quotes should not be escaped. You don't escape them on the command line, either, do you?
logpath = LoggerUtils.getProperties().getProperty("log.path");
System.out.println("logpath: " + logpath);
The above code returns:
logpath: C:UsersMauriceDesktopLogs
In the properties file is:
log.path C:\Users\Maurice\Desktop\Logs
How do I retain the file separators? I want this to work on Linux as well and not just Windows.
Actually, you need to put this in the property file:
log.path C:\\Users\\Maurice\\Desktop\\Logs
See this:
http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html
more precisely the load method:
http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader)
Scroll down a bit and you will see this among other things:
The method does not treat a backslash character, \, before a non-valid escape character as an error; the backslash is silently dropped. For example, in a Java string the sequence "\z" would cause a compile time error. In contrast, this method silently drops the backslash. Therefore, this method treats the two character sequence "\b" as equivalent to the single character 'b'.
Backslash \ is an escape character that is silently dropped otherwise.
In a property file, you need to either use forward slashes:
C:/Users/Maurice/Desktop/Logs
Or, escaped backslashes:
C:\\Users\\Maurice\\Desktop\\Logs
You need to escape the slashes as they are special characters. See: Java Properties backslash
The Java properties file format dictates that the backslash character ("\") escapes the character that follow it, so to get a literal windows path you must have:
logpath: C:\\Users\\Maurice\\Desktop\\Logs
However, Java will convert path separator characters for you automatically to suit the runtime platform, so you can avoid this nuisance by always using forward slashes:
logpath: C:/Users/Maurice/Desktop/Logs
You can store the Properties to file first, then load it again to use. Properties will take care of escaping/ unescaping anything.