• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

how to change current (working) directory?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic