开发者

Reading Java command line arguments on Unix

开发者 https://www.devze.com 2023-01-04 14:51 出处:网络
I am trying to read a folder name from command line for a Java application on Unix. I have a ksh script (test.ksh) and it has the line:

I am trying to read a folder name from command line for a Java application on Unix.

I have a ksh script (test.ksh) and it has the line:

java SomApp开发者_Python百科 $*

I am trying to execute this as below:

test.ksh ../folder

But this fails; I mean my Java application says there is nothing under "../folder" though there are some files.

How to read such relative paths on Unix?


It's not entirely clear what you're trying to do.

--snip--

If you want each filename under your folder as an argument to your command, try something like

java SomeApp `ls ../folder`

...which will execute the ls command against ../folder and substitute the output (the list of files under ../folder).


Here is a simple program to list the files in a directory specified on the command line.

public static void main(String... argv) {
  File[] list = new File(argv[0]).listFiles();
  for (File f : list) {
    System.out.println(f.getAbsolutePath());
  }
}


The key question is "Does the Java application expect a directory name or a list of file names?"

If the application expects a directory name, you appear to have given it a directory name. Unless your shell script does something unexpected such as change working directory (cd), the problem is most likely in the application.

If the application expects a list of file names, then you should pass it file names and not a directory name. That might be just:

./test.ksh ../folder/*

Or you might need to be more selective if the folder also contains sub-folders and the application will be upset by folder names instead of file names.

0

精彩评论

暂无评论...
验证码 换一张
取 消