-1

I'm trying to build a command line interpreter that implements "cd" command in linux. I don't know how the function that suppose to do that shoud work.

I know that this question has already been answered here: Changing the current working directory in Java?

But I'm new to Java and I read alot about this and still don't get how new File(parent, path) would help me change my directory. Any help please?

Community
  • 1
  • 1
dili
  • 31
  • 8
  • Have you read the other answers, like: http://stackoverflow.com/a/13981910/829571 – assylias Oct 29 '14 at 13:21
  • Yes. But I'm trying to build a cli that implements "cd" command in linux. I just wanna change build a function that change the current dirctory without further working. – dili Oct 29 '14 at 13:25
  • There's quite a few ways to get what you want and, yes, one of them is changing the current directory but I wouldn't recommend going that way. You might want to keep track of the directory path within your application instead and be able to use the methods taking the parent folder path as argument. – Jonathan Drapeau Oct 29 '14 at 13:29
  • Also, I would recommend that you read [ask] as your question right now is quite broad. – Jonathan Drapeau Oct 29 '14 at 13:34
  • I editted the question. – dili Oct 29 '14 at 13:36
  • I am trying to implement a Java based shell as well and the "cd" just needs to work for the duration of the program. I can get "pwd", "copy", "mkdir" etc all working, the missing command to be implemented is "cd". – Levon Nov 16 '14 at 14:13
  • @Levon After all I used a global variable that holds the path of my current direction. "cd" would just change it and all other commands use/modify it accordingly. Nothing simpler I guess. – dili Nov 17 '14 at 20:17

2 Answers2

1

It won't help you change the directory; it is what is advised to use instead of changing the current working directory. The position of the designers of Java is that, if some part of your application needs a specific context directory other than the current working directory inherited from the underlying OS, this should be done by relying on the explicit two-argument File constructor (and likewise for any other file-based APIs).

Note that this makes a lot of sense because the current working directory is effectively global state, and mutable global state is a well-knows Pandora's box of untraceable bugs.

Marko Topolnik
  • 179,046
  • 25
  • 276
  • 399
1

I used a global variable that holds the path of my current direction. cd would just change it and all other commands use/modify it accordingly. Nothing simpler I guess.

dili
  • 31
  • 8