Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

This my be harder than it sounds  RSS feed

 
Jason Domes
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a program in java that will open cmd.exe and change the directory to my java projects directory. I know this may sound silly but I figured I have to start somewhere. I don't even know where to begin trying to write the code for this so any input is greatly appreciated.

Many thanks in advance
Jason D.
 
Marilyn de Queiroz
Sheriff
Posts: 9080
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to open a command prompt to change directories. What do you plan to do after you cd to the new directory?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no system wide "this is the current directory" setting on Windows. It doesn't make much sense to open a command prompt and make it "cd" to any directory, because as soon as the command prompt closes again, the setting has "disappeared" again.

Just use the complete path in your Java application.
 
Clifton Craig
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,

What you're trying sounds simple from the onset but consider it deeper. You wish to automate the actions of another program. That in itself is an advanced topic because the program in question, cmd.exe, would have to allow automation. In other words the program in question would have to expose some API (application programming interface) or command line parameters that control the automated functionality. It's been a while since I've worked on Windows so I can't tell you if cmd.exe supports command line parameters that control it's initial directory. For a beginner let's start somewhere more simplistic. Maybe your "hello world" program can list the files in your Java projects directory to the command window it was launched from? Something like:


That's off the top of my head and I haven't compiled it or tested it but it should work mostly. I would start there, coding the above snippet into a file named ListFiles.java and then run the compiler: on the command line (in cmd.exe). Look in the current directory afterwards to make sure ListFiles.class was created then execute: on the same command line to see it run.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!