From java code file getting created with a lock symbol in Linux - java

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);

Related

How do I make my directory give the executable permissions to script files which are going to be created by my Java program?

I want my directory to give executable permissions (by default) to all the shell scripts which are going to be created in that directory after I run my Java program. I tried the following commands:
setfacl -d -m group:name:rwx /path/to/your/dir
find ./ -name "*.sh" -exec chmod +x {} \;
The first one is not showing any response while the second one works fine but I have to execute this command manually in terminal after my Java program has created all the scripts. This is not what i seek. I want this thing to be automatic. Here is what I am trying to achieve:
My Java program creates the .sh files in a directory.
Now the program would try to execute this script file.
Here is a Java code snippet which shows how it is going to execute the script files:
ExecuteShellComand obj = new ExecuteShellComand();
String command2 = "./script.sh";
String output2 = obj.executeCommand(command2);
It doesn't run unless I give the executable permissions to the script.sh. How do I do it? Is there any way around it? If I am not doing something in a way it should be done, feel free to give your suggestions. Thanks
Default ACL permissions are masked by the file's creation mode as specified in open and mkdir syscalls.
Since files are usually created with a default mode of 0666, execute permissions will be masked instead of inherited.
It's the responsibility of the program that creates the files to create them with the right permissions. If you set the permissions correctly when creating the scripts, you won't need ACL or chmod at all. The best way to fix this would be for your program to set the mode in the open call to 0777.
Java appears to have Files.createFile for this. Additionally, you have a more fuzzy File.setExecutable to do it after the fact, which is not Unix canonical behavior, but probably fine for your use case.

Sudo Java calling C app and Permissions

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.

can not read file from linux system using java

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!!

Open a PDF file on a remote machine with local app in Java?

When I run code like this in Mac os:
Runtime.getRuntime.exec("open testFile.pdf");
the Mac OS will run Acrobat to open the local PDF file.
How can I do it, when the file is on a remote machine?
\\remoteHost\share\testFile.pdf
I try to do that like this:
Runtime.getRuntime.exec("open \\\\remoteHost\\share\\testFile.pdf");
but I failed.
Thanks!
Runtime.getRuntime().exec(String) just passes the String to the OS for it to handle it. So, if you want to run the command
open \\remoteHost\share\testFile.pdf
You have to pass it to exec(). Remember to escape the \ (replace every \ by \\)
Runtime.getRuntime().exec("open \\\\remoteHost\\share\\testFile.pdf");
Of course, the user running the program should have permissions in the remote machine. If you need to set another user, use the open program command line parameters.
I don't think it's possible to directly specify the path of an SMB share while using the open command. If you want to use open, you would have to mount that share as a local directory. See http://osxdaily.com/2009/09/24/access-and-mount-an-smb-share-via-command-line/ for an explanation.
A solution like that would be pretty fragile though. If you want a more reliable solution, I'd suggest using JCIFS (http://jcifs.samba.org/) to download the file locally and then use
Desktop.getDesktop().open(pdfFile);
to open the downloaded file.

Runtime exec output path

I am trying to run a perl command with Java runtime exec in linux/ubuntu/gnome. The command generates an pdf file, but it saves it in my home folder. Is there any way that the exec method can set an output path for the commands executed? Thanks in advance.
The exec method just runs the command on the operating system, so you'll want to change the command you're running with exec more than anything in "Java" per se.
There are a few possibilities:
Change the working directory of your java program. The .pdf is being saved in your working directory because this is where the program is being run.
Unfortunately it's not simple to change this value after the program has been launched. It is, however, trivial to change before the program starts; just change the working directory before starting the program.
Move the file to it's desired location after it's been created in your home directory.
Change the command so that it includes the target location. Your perl script may have an option that will enable you to save it's output to a certain location (usually -o or --output). Using this your program would change from:
Runtime.exec("perl someprogram");
to something like:
Runtime.exec("perl someprogram -o /path/to/some.file")
You might be able to use "output redirection", if there is no option to do this.
Try something like what's below as your argument:
Runtime.exec("perl someprogram > /path/to/some.file")
Unfortunately, without knowing more details of your situation I can't provide more concrete advice.
While each approach has benefits and drawbacks, it's probably best to just implement the one that you understand best; if you can't get one to work, try another.
A good, free online resource for learning is Introduction to Linux: A Hands On Guide.
Section 2.2 has details on cd which you can use for 1..
Section 3.3, section 3 teaches about the mv command, which will be useful in 2..
Section 5.1 is about I/O redirection. Knowing about "output redirection" and the > operator, are important for 4..
For 3., you'll have to consult the documentation of the perl program you're using.
You could modify the Perl script to accept an absolute path for the output.
You can trying setting the working directory using exec(java.lang.String[], java.lang.String[], java.io.File) where File is the directory the command is executed from.
If all else fails, you'll can always copy the generated file from the Home directory to your final location.

Categories

Resources