Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stop Java from automatically interpreting a passed String argument that looks like a Path wildcard?  RSS feed

 
John Jeffersonian
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a simple program that outputs the arguments you give to it:

If you run this program passing "C:\*" as an argument:
Instead of outputting a single String "C:\*" Java will automatically interpret that as a wildcard and will instead output the contents of your C:\ folder, so instead of the args String array containing only 1 element, it contains a separate element for each file in your C:\ folder.

Normally that would be a smart thing to do but I'm writing my own implementation of DIR, and a user will sometimes pass multiple paths, some as wildcards and some normally, like: "java com.company.Main C:\* C:\Users\John John", and if I have multiple wildcard expressions in the passed arguments I have no way differentiating between an actual path specified by the user and an argument that Java automatically interpreted from a wildcard. Is there any way to turn this off and just have all arguments always be Strings?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jeffersonian wrote:Stop Java from automatically interpreting a passed String argument that looks like a Path wildcard?


Unfortunately, it is not the JVM that is processing the command line wildcards. The wildcards are processed by the shell prior to calling the JVM.

You will need to looking into the shell that you are using, and modify your command line accordingly.

Henry
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd have to teach your users to type something like...

C:\> java com.company.Main "C:\*" "C:\Path With Spaces" C:\Users\John John

...which probably won't go over very well.
 
John Jeffersonian
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Drats! Oh well. Thanks for the clarification.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!