aspose file tools*
The moose likes Java in General and the fly likes Executing a Windows Command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Executing a Windows Command" Watch "Executing a Windows Command" New topic
Author

Executing a Windows Command

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
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: 39393
    
  28
How are you emptying the output and error streams from that process?
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
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: 4181
    
  21

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: 18651
    
    8

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: 217
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: 217
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: 4181
    
  21

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: 18651
    
    8

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: 217
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
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
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: 39393
    
  28
I thought this discussion would become too difficult for “beginning”. It can be moved.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Executing a Windows Command