Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

I've Been Globbed!

800308Sep 17 2006 — edited Sep 17 2006
Folks,

Does suns-windows JAVA.EXE glob it's command line arguements? (ie: does it expand wildcard characters into matching filenames)

All unix shells glob, but windows CMD.EXE doesn't, so it would make sense for java.exe to mask this inconsistency by doing the globbing itself?

... it's just that after doing the work to glob args myself, I was stunned to find that the command
java listFiles c:\tmp c:\tmp\*.java *.txt
produced the main(String[] argv)
c:\tmp c:\tmp c:\tmp\blah.java c:\tmp\Noname2.java semicolon.txt
can anyone confirm this behaviour

Details ----------------------

listFiles.java
import java.io.File;

//http://jakarta.apache.org/oro/index.html
//CLASSPATH=%CLASSPATH%;C:\Java\lib\jakarta-oro-2.0.8\jakarta-oro-2.0.8.jar
import org.apache.oro.io.GlobFilenameFilter;
import org.apache.oro.text.GlobCompiler;

class listFiles
{
	private static final boolean DEBUGGING = false;
	private static void DEBUGPRINT(String msg) {
		if (DEBUGGING) {
			System.out.println("#DEBUG: " + msg);
		}
	}

	private static void printList(String[] list) {
		if (list == null) return;
		for(int i=0; i < list.length; i++)
			System.out.println(list);
}

private static String joinList(String[] list) {
return(joinList(list, " "));
}
private static String joinList(String[] list, String seperator) {
if (list == null) return("");
StringBuffer s = new StringBuffer (list[0]);
for(int i=0; i<list.length; i++)
s.append(seperator + list[i]);
return (s.toString());
}

private static String[] listFiles(String name) {
// names of all files "matching" the given name
String[] names = null;
String[] tmp = new String[1];
File file = new File(name);
if (file.isFile()) { //file --> single name
DEBUGPRINT(name+" is a file");
tmp[0] = file.getName();
names = tmp;
} else if (file.isDirectory()) { //directory --> list of names
DEBUGPRINT(name+" is a directory");
names = file.list();
} else if (!file.exists()) { //pattern --> list of names
DEBUGPRINT(name+" does not exist");
File dir;
String fileName;
if ( file.getParent() == null ) {
dir = new File(System.getProperty("user.dir"));
fileName = name;
} else {
dir = new File(file.getParent());
fileName = file.getName();
}
DEBUGPRINT("dir="+dir.getPath()+", name="+fileName);
GlobFilenameFilter filter = new GlobFilenameFilter(fileName, GlobCompiler.STAR_CANNOT_MATCH_NULL_MASK);
names = dir.list(filter);
} else {
System.out.println("#WARNING: unknown type: " + name);
names = tmp;
}
return(names);
}

public static void main(String[] argv)
{
System.out.println("#ARGS: " + joinList(argv));
for (int i=0; i<argv.length; i++) {
System.out.println("\n#LISTING: " + argv[i]);
printList(listFiles(argv[i]));
}
}
}run with
java listFiles c:\tmp c:\tmp\*.java *.txt
produces
#ARGS: c:\tmp c:\tmp c:\tmp\blah.java c:\tmp\Noname2.java semicolon.txt

#LISTING: c:\tmp
blah.class
blah.java
cpp_tutorial
krc
MainClass.class
MyInt.class
nice_ms_webpage.default.aspx
Noname2.java
set.tmp
SubClass.class

#LISTING: c:\tmp\blah.java
blah.java

#LISTING: c:\tmp\Noname2.java
Noname2.java

#LISTING: semicolon.txt
semicolon.txt
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 15 2006
Added on Sep 17 2006
3 comments
112 views