• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to change current (working) directory?

 
Dennis Spathis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Under JDK 1.2.2, how can one change the current (working)
directory?
Dennis
 
Dennis Spathis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI:
This cannot be done in JDK 1.2.2, but it is possible in JDK 1.3 (in the latter, there is a new Runtime.exec() methods that
takes a working directory as an argument).
 
Paul Michael
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm i didn't know it was possible until i saw this post. i have seen a lot of mock exams asking this question and the answers always say that "there is no way in java that you can change the current working directory with Files.." but that was i think before 1.3 came along...
 
Jan Sauerwein
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh?
Hi first of all.
What do you mean with "changing the current directory"?
When you want to save or load some files, there is no problem to get there base.
But when you want to change the directory which is given by the methods, for example: getCodeBase ( ):URL or getDocumentBase ( ):URL, it isn't possible to change it, cause the Applet is there and wouldn't move for you. Also the directory base couldn't changed with jdk1.2.x.
But take a look at System.getPrperties ( ) and System.setProperties. Espacialy at the properties user.dir, user.home They could be set and you can use them to navigate from a known point in the file-system to save and load the needed data.
I hope it helps.
j.a.n.s
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Runtime exec() overload added in 1.3 only allows you to set the working directory for the new process which is being created by the exec command. This has no effect on the working directory which Java is using. But Jan has the right idea. Try running the following code - note that getting the "user.dir" property, and printing the absolute path of a File "", are just two ways of accomplishing the same thing - finding out the current working directory. I include both for completeness.
<code></pre>
System.out.println("Current working directory:");
System.out.println(System.getProperty("user.dir"));
System.out.println(new File(".").getAbsolutePath());

System.out.println("Changing working directory...");
System.setProperty("user.dir", "C:\\Java");

System.out.println("Current working directory:");
System.out.println(System.getProperty("user.dir"));
System.out.println(new File(".").getAbsolutePath());
</pre></code>
[This message has been edited by Jim Yingst (edited July 08, 2001).]
 
Oliver Jonas
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this is a fully working solution as the sample code below should print "true" for *both* if the working directory was really changed:
System.out.println("Current working directory:");
System.out.println(System.getProperty("user.dir"));
System.out.println(new File(".").getAbsolutePath());
System.out.println("Changing working directory...");
System.setProperty("user.dir", "C:\\tomcat");
System.out.println("Current working directory:");
System.out.println(System.getProperty("user.dir"));
System.out.println(new File(".").getAbsolutePath());
File f1 = new File("LICENSE");
System.out.println(f1.exists()); // prints false
File f2 = new File("C:\\tomcat\\LICENSE");
System.out.println(f2.exists()); // prints true

I guess the only possible solution left is calling "cd" command using exec().
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Oliver Jonas:

I guess the only possible solution left is calling "cd" command using exec().


That won't work, either.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!