| Author |
Executing a Windows Command
|
Dennis Putnam
Ranch Hand
Joined: Feb 03, 2012
Posts: 208
|
|
I am trying to run the 'attrib' command to make a created file hidden. I get no errors but the file never gets the hidden attribute (works just fine from the command line).
Can someone spot what I am doing wrong or suggest how to debug this? TIA.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
|
How are you emptying the output and error streams from that process?
|
 |
Dennis Putnam
Ranch Hand
Joined: Feb 03, 2012
Posts: 208
|
|
|
Thanks for the reply but in this case there is no output stream to empty. This try/catch immediately follows a try/catch for 'file.createNewFile()'. Data will be written to the file a some other point.
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
That's not the point. Every process has its own output and error streams. You need to consume those streams for a couple of reasons (to find out what feedback is provided if - for example - the processes didn't work, and because a process can freeze if the output or error streams fill without being consumed.)
You should read Deconta's great article on this: When Runtime.exec() won't.
|
Steve
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Could I suggest just using the setAttribute method of the java.nio.file.Files class? I notice that the API documentation for that method includes
Usage Example: Suppose we want to set the DOS "hidden" attribute...
|
 |
Dennis Putnam
Ranch Hand
Joined: Feb 03, 2012
Posts: 208
|
|
Paul: Thanks. I'll take a look at that.
Steve: I thought I understood the article but it didn't seem to matter. Here is what I have now (this may become academic after I look at Paul's suggestion):
|
 |
Dennis Putnam
Ranch Hand
Joined: Feb 03, 2012
Posts: 208
|
|
Paul,
Thanks, that worked, sort of. When I did a search for this specific issue, all the solutions that came up were the convoluted 'exec' method. Why were so many people unaware of your method? Anyway, the problem I have now seems to be a Windows issue but it makes no sense and I am wondering if Java is interjecting itself. When I try to write the file I am getting a permission denied error. I can write to a hidden file from Windows so it is not clear why I get this. Since it is so easy, I will try turning that off and back on when I need to write the file to see what happens.
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
Dennis Putnam wrote:Paul,
Thanks, that worked, sort of. When I did a search for this specific issue, all the solutions that came up were the convoluted 'exec' method. Why were so many people unaware of your method? Anyway, the problem I have now seems to be a Windows issue but it makes no sense and I am wondering if Java is interjecting itself. When I try to write the file I am getting a permission denied error. I can write to a hidden file from Windows so it is not clear why I get this. Since it is so easy, I will try turning that off and back on when I need to write the file to see what happens.
That is new - added in Java7.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Steve Luke wrote:That is new - added in Java7.
That's right. And it was not one of the high-profile features, so very few people know about it. But I did know that Dennis was using Java 7 because of this line of code:
|
 |
Dennis Putnam
Ranch Hand
Joined: Feb 03, 2012
Posts: 208
|
|
|
Ah, that explains it. What it does not explain is why Java would imposes its own file restrictions contrary to the OS. By turning the hidden attribute off and on I can write to the file. Obviously that means it is Java that is prohibiting writes to a hidden file and returning a false permission denied error.
|
 |
Ivan Jozsef Balazs
Ranch Hand
Joined: May 22, 2012
Posts: 380
|
|
Dennis Putnam wrote:What it does not explain is why Java would imposes its own file restrictions contrary to the OS. By turning the hidden attribute off and on I can write to the file. Obviously that means it is Java that is prohibiting writes to a hidden file and returning a false permission denied error.
Do you mean a file remains writable on the OS level when the hidden attribute is set, whereas Java handles it as not writable?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
|
I thought this discussion would become too difficult for “beginning”. It can be moved.
|
 |
 |
|
|
subject: Executing a Windows Command
|
|
|