• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing a Windows Command

 
Dennis Putnam
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you emptying the output and error streams from that process?
 
Dennis Putnam
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20971
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20971
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought this discussion would become too difficult for “beginning”. It can be moved.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic