I am trying to read/write files on Ubuntu 12.04.
I set permission of that directory by chmod -R 777 .
But still when I call canRead() method on that directory it returns false.
my directory is /root/Temp
please help me to solve this problem
Code (copied from comments):
File xyz = new File("/root/Temp");
System.out.println("filename :"+xyz.getPath());
System.out.println("can read :"+xyz.canRead());
String[] children = xyz.list();
Children is null, output of can read is false.
Are you running your program as root? it is not sufficient changing the permissions of /root/temp, if you are not the user root you wont be able to "go through" the dir /root unless you also change the permissions of the dir /root .
I too faced the same while doing XML Parsing using Java SAX Parser. My file is not read by the java program. The mistake I done was, I didn't specify the file name correctly.
After your /root/Temp you have to add some more details too.
For example : /root/Temp/example.xml
Then your program will work fine.
Hope this helps!!
Related
So my Ubuntu terminal is claiming that the java file I'm trying to run does not exist. However, it clearly does, and it looks like I'm in the right directory. Here's what I'm trying to compile
And here's what happens
Can anybody help me with this?
If you are sure that you are in the right directory, BE SURE that you write the filename Exactly as what it is. Because Linux is case sensitive and for example, if you have a file with name "Sample" and you try to open "sample" file, you will get the error.
if you type manually, try to use Tab button in your keyboard. it will auto-Complete the file/dir name for you. With this way, if you have typo or wrong spelling, Tab does not complete the name and you will know that there is something wrong with what you type in terminal.
Another comment suggested that I use "ls" to see if the terminal can see the file. I got a permission denied error, and used
find . -type d -exec chmod 755 {} \;
to resolve this.
A weird question:
I am running eclipse on windows and I am trying to open a file with a hard coded path:
String inputFile = "C:/temp/abc.txt";
File folder = new File(inputFile );
When I run this I get error:
java.io.FileNotFoundException: C:/temp/abc.txt (The system cannot find the file specified)
I have local admin rights on this windows 10 machine. I have tried running Eclipse as a Administrator but it doesn't resolve the issue.
The only way I can get by is if I traverse to C:/temp/ in cygwin and do a chmod 777 * . Then my program is able to open the file okay.
The work around should be fine if I was just reading a simple file but I am also creating files from within my java project which it does successfully create but then when it comes to reading them it fails.
myFile.setReadable(true); // doesnt work either.
Is this some sort of windows permissions issue ? Could it be that cygwin has taken over some admin rights on the file system? I have tried stopping it but the issue persists. Or is this an eclipse setting?
Many Thanks,
-A
It was a windows permissions issue.
Right click on the folder -> Properties -> Security -> Edit -> Add -> Everyone.
I dont understand why I need to add every one if I am the local admin and I launched eclipse but frankly I dont care about the bizarre world of windows.
Thanks to all those who tried helping.
Cheers
check this.
Permission Denied: File Creation in Java
https://www.mkyong.com/java/how-to-set-the-file-permission-in-java/
I am trying to write to a file using a FileOutputStream in java. I am running ubuntu and I think the problem is with the permissions. Even though the error I receive says '(No such file or directory)' I am sure the path I am giving is right, since I can read from the same file with no issues. I am not very familiar with ubuntu write permissions but I think I need to set the mode with chmod -r on the root directory. However I am not sure of the exact command and the option of permission I should set.
I am calling a C application (console only) from my Java application.
I am calling it with: Process proc = rt.exec("./Debug/CPP_CL --device 0");
The CPP_CL needs access to clinfo() hardware .. so the GPU hardware as its processing on the GPU's. Hence, in this case needs to run as sudo/root.
Its all working fine at the moment but only if I run the Java JAR as sudo. Currently for testing only the CPG is chmod 777 (I know bad).
What I would like to know is what’s the best way to do this ? Will the CPP run as SUDO if called by SUDO java ? Or does it need to be chmod'ed ? If so what’s the best chmod value ?
Thanks.
Running Java with root is, as you said, one possibilty, but not exactly good.
The usual chmod flags (rwx) too won´t help you.
Just call it with a sudo won´t solve anything. Usually, a password is required, and if the java program can enter it (ie. it knows the root password) ... well, then it´s the same as above again.
As said in the comments, you can add a exception to sudo, but there are some catches:
You can only specify a program/script file, but no parameter limitation. You will need a script file which calls ./Debug/CPP_CL --device 0 (better with full path) and add the batch file as exception.
Furthermore, you have to make sure that the script file can´t be modified by users (chmod of the file) and can´t be deleted (chmod of the containing directory). File modification would mean that the modifying user can put anything in it and run it as root, and deletion would let the user place another file there with this name = same effect. Given that, you can call with with sudo.
If you wnat to call it without sudo, make another script file which just calls file 1 with sudo.
Another possibility is the special chmod flag SUID on the program itself (if it is enabled/supported in your distro). But here again, you can´t limit the parameters.
About the data files: A file created by a root program will be owned by root. chmod/chown as root can change that. If you only need to read the file, default umasks will allow that on many systems (if the files are in not-only-root-directories like /root)
Answer:
https://unix.stackexchange.com/questions/18830/how-to-run-a-specific-program-as-root-without-a-password-prompt
This worked.. I was able to sudo from Java and with the above no PWD is required for that application.
From java, I use the following code to create a file:
File dirName = new File("/var/www/html/nyk/app/webroot/MusicDB/music.db");
But the file gets created with a 'lock symbol' on top of it which indicates restricted permissions. I am able to change the permissions of this file manually from terminal using cgmod -R 777 filename . But I am using another code in the same program, which copies the created file to another destination. Due to the restricted permission, it is not able to copy the file.
How can I create the file eliminating the restricted permsiion issue in first place?
have you tried this?
myFile.setReadable(true);
myFile.setWritable(true);
You can use the File.setReadable(), File.setWritable() methods for that! You can either grant the permission, or revoke them as per your needs! Currently in your case, you need to provide true to grant the permissions!
Or a dirty workaround would be
Runtime.getRuntime().exec("chmod 777 file")
You could either use umask outside of Java before starting the application.
umask 000
or use
dirName.setReadable(true);
dirName.setWritable(true);