I am wondering how to pass a file
as an argument
on linux command line
.
public class Main {
public static void main(String[] args){
System.out.println(args[0]);
}
}
For the above code if I do:
java -jar myJava.jar blah.txt
It prints blah.txt
to the screen.
But I have a sample line of code that looks like this:
java -jar myJava.jar < blah.txt
How am I able to get the value of blah.txt
from the above command?
Use one of the techniques for reading from System.in
where the file is being redirected such as Scanner
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
...
}
try the following
java -jar myJava.jar 'blah.txt'
The quotes indicate that the argument is literal.
double quotes will also work, but will not prevent things like variable expansion, so single quotes is better when trying to pass a literal string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.