• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Executing a Windows Command

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you emptying the output and error streams from that process?
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought this discussion would become too difficult for “beginning”. It can be moved.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic