Skip to Main Content

New to Java

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!

Read in file name + path using command line arguments

Mahsa NouriJun 6 2012 — edited Jun 6 2012
Hi guys,
I am trying to use command line arguments to read in the name of two files. The code below works as long as the files are on the class path.
public class cmdLineArguments {
	
	public static void main(String[] args) {
		String xmlFile1 = args[0];
                String xmlFile2 = args[1];
			
		System.out.println ("\nXmlFile1: " + args[0] + "\nXmlFile2: " + args[1]);
	}
}
I would like to be able to read files that are not on the class path as well by entering their full path along with their names.
In other words, It works if I type:
C:\Documents and Settings\user\workspace\CmdLineArgs\src>java cmdLineArgs file1.xml file2.xml
but not if I type:
C:\Documents and Settings\user\workspace\CmdLineArgs\src>java cmdLineArgs c:\Documents and Settings\user\desktop\file1.xml c:\Documents and Settings\user\desktop\file2.xml
Would anyone please explain how I can edit the code to make it work? I guess I could take them both as a String, modify them manually (add a \ to it etc) but is there an easier way to do it? Even a reference to a tutorial, article etc about this would be very much appreciated.

Also, any idea why a code would compile and run fine in Eclipse but would create an error from command line?

Thanks a lot.
This post has been answered by sabre150 on Jun 6 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 4 2012
Added on Jun 6 2012
5 comments
2,004 views