Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Create a file on Linux using Java code

 
Ranch Hand
Posts: 227
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a file on Linux machines using Java code.

Using the code I am able to create a file on Windows but the jar does not work on Linux platform.

Cant we create a flat file on Linux using the same code ?
 
Marshal
Posts: 69752
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain more; we can only help if we know all the details. There should not be any difference between creating a file on Windows® and on Linux; the PATHs have a different format, obviously. Tell us how you are trying to create such a file. Also find out whether the location you are trying to create it is accessible for writing or not.
 
Santosh Kumar Nayak
Ranch Hand
Posts: 227
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

the jar does not work on Linux platform.


Does Linux machine configured properly to run simple java code/jar?

Thanks Campbell Ritchie for clarifying about Path.

Thanks Santosh Kumar Nayak for sharing small piece of code.

I write following code with the help of Google search:-

Might it helpful for newbies!
 
Santosh Kumar Nayak
Ranch Hand
Posts: 227
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Linux machine is configured properly but the above code is not able to generate a *.txt file on Linux Box.
 
lowercase baba
Posts: 12870
62
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are the permissions on the directory set to allow writing to the dir?
 
Campbell Ritchie
Marshal
Posts: 69752
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/root/Desktop???

That is most unlikely to be accessible for writing. I suggest you change /root to a tilde and try writing it on your desktop ~/desktop/...
Please confirm whether it is desktop or Desktop; it matters on Linux.

That code needs a bit of editing. You don't need the two try blocks; apart from the names of the files they are identical. You should also close the writers in a finally (or use try‑with‑resources).
 
Saloon Keeper
Posts: 22248
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the "tilde" thing is a shell artifact and it won't work properly within Java program code. However there's a java.lang.System Property that returns the path of the current user's home directory. It can be used to construct an appropriate File object. Something similar to:



Note, by the way that that particular code works equally well regardless of whether it's running on Windows or on Linux.
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic