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
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.
I have a Java program which I'm executing in a Linux environment through a bash script.
This is my simple bash script, which accepts a String.
#!/bin/bash
java -cp com.QuoteTester $1
The issue is that the command line argument can be with Spaces or Without spaces.
For example it can be either:
Apple Inc. 2013 Jul 05 395.00 Call
OR
Apple
My code is:
public static void main(String[] args)
{
String symbol = args[0];
if (symbol.trim().contains(" ")) // Option
{
}
else // Stock
{
}
}
So the issue is that , when I am trying to execute it this way:
./quotetester Apple Inc. 2013 Jul 05 395.00 Call
its only always going to the else condition that is Stock .
Is there anyway I can resolve this?
When you pass command line arguments with spaces, they are taken as space separated arguments, and are splitted on space. So, you don't actually have a single argument, but multiple arguments.
If you want to pass arguments with spaces, use quotes:
java classname "Apple Inc. 2013 Jul 05 395.00 Call"
This is not a Java issue per se. It's a shell issue, and applies to anything you invoke with such arguments. Your shell is splitting up the arguments and feeding them separately to the Java process.
You have to quote the arguments such that the shell doesn't split them up. e.g.
$ java -cp ... "Apple Inc. 2013"
etc.
See here for a longer discussion.
The arguments are handled by the shell , so any terminal settings should not affect this. You just need to have quoted argument and it should work.
Single quotes are the best option
Spaces and double quotes can be resolved this way.
java QuerySystem '((group = "infra") & (last-modified > "2 years ago"))'
In the original question the OP is using a shell script to call a java command line and would like the shell script to pass the arguments without performing the Blank interpretation (Word Splitting) option of input interpretation
https://rg1-teaching.mpi-inf.mpg.de/unixffb-ss98/quoting-guide.html#para:sh-ifs
If you know how many arguments there are then you can double quote the arguments
#!/bin/bash
java -cp com.QuoteTester "$1"
So you call this script, save as quotetester.sh.
./quotetester.sh "hello world"
and "hello world" gets passed as a single argument to Java. You could also use
./quotetester.sh hello\ world
with the same effect.
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
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?
I want to attach meta data to a file in Unix file system.
attr command lets me do that but
the command syntax requires the path of the attached variable to be in double qoutes.
attr -s outpipe0 "/mnt/FUse/FileB" FileA
how can i Use System.Runtime.exec in java to run the above command. When ever i try to run using a string array argument I have to give the above "/mnt/FUse/FileB" which causes problem in java program as it considers the double quotes as end of string in java. I basically want to send a string argument which in itself has double quotes.
Can someone suggest a work around .
Thanks
You can escape the quotes within your literal string in Java, like this:
"\"/mnt/FUse/FileB\""
That will address your question of how to include double quotes in a string, but I doubt it will solve your program. That's because I doubt the attr program actually wants (or accepts) double quotes. Instead, the shell eats them. For example, if the command you type in the shell is the one you mentioned, the double quotes will be consumed by the shell before the arguments are passed to attr. So I have doubts that you need the double quotes at all (but if you do, see above).