set execute permition on file [duplicate] - java

This question already has answers here:
How do I programmatically change file permissions?
(12 answers)
How to change image permission mode to 777 using Java code?
(5 answers)
Closed 7 years ago.
I need to set execute permition on Linux script file using java 1.4. I would prefer to use java native library. If it is not possible with native library what is lightweight lib that allows to do it?

JAVA5 and prior there is workaround using exec extracted from here:
public static void runCmd (String[] cmd) {
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader r = new BufferedReader(
new InputStreamReader (
p.getInputStream()
)
);
} catch(Exception e) {
}
}
USE
runCmd(new String[] {
"/bin/chmod",
"755",
"/path/to/your/script"
});
By terminal in Linux
UP TO JAVA6
For permissions use File::setExecutable(boolean [, boolean])
File file = new File("/your/path/to/file/file_script.sh");
file.setExecutable(true);
Check this MKYONG permissions tutorial

Related

Class use txt file in the same jar NPE [duplicate]

This question already has answers here:
How to read a file from jar in Java?
(6 answers)
How to read a text file inside a JAR? [duplicate]
(4 answers)
Closed 3 years ago.
I have class files and a text file wrapped up in a jar. This problem has been solved on the internet before, but when I try it I get a null pointer exception, or File f.exists() returns false. It should be noted that my code is not in a package. It should be noted that when help.txt is dropped in the same folder as the jar, then it works.
`MyClass z = new MyClass();
String helpPath = z.getClass().getClassLoader().getResource("help.txt").toString();
File f = new File(helpPath);
if (f.exists()){
Desktop d = Desktop.getDesktop();
d.open(f);`
It should also be noted that I have code written to open powershell and then java my class file, with no specified classpath.
` Runtime.getRuntime().exec(new String[]{"cmd","/c","start","powershell","-noexit","/c","java -jar \"" + filename + "\""});`

Jar accessing to external files [duplicate]

This question already has answers here:
Read properties file outside JAR file
(8 answers)
How to get the path of a running JAR file?
(33 answers)
Closed 6 years ago.
I have a folder structured like this:
MyFolder:
file1.xml
file2.xml
project.jar
But if in a class I use:
File f = new File("file1.xml");
I receive an error, because it doesnt find the file. Why?
You should use a relative path in your code.
Example: File f = new File("./file1.xml");
If you are using Windows the code you posted will work, but not on Linux where the default parent file is your home.
But you can do in any OS by using:
public class MyClass {
public void loadFile() {
URL url = MyClass.class.getProtectionDomain().getCodeSource().getLocation();
File jar = new File(url.toURI());
File f = new File(jar.getParent(), "file1.xml");
//your code
}
}
PS: This needs to be inside project.jar because you are getting the location where you jar file is.

Why does java.io.File not implement Autocloseable? [duplicate]

This question already has answers here:
Why doesn't java.io.File have a close method?
(6 answers)
Closed 8 years ago.
After upgrading to Java 7 I get the following code flagged by Eclipse:
try (File file = new File(FILE_NAME)) {
file.delete();
}
Error is:
The resource type File does not implement java.lang.AutoCloseable
And Java's documentation doesn't have File listed in the AutoCloseable docs:
http://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html
So besides adding the catch block, what is the suggested alternative?
As Jeffrey said in the comment to the question, you need to differentiate between a File and an InputStream, e.g. FileInputStream.
There is nothing to close in a File, but there is something to close in a stream or a reader.
try (FileInputStream fs = new FileInputStream (new File(FILE_NAME))) {
// do what you want with the stream
}

".bat" file wont open in JAVA [duplicate]

This question already has answers here:
How do I run a batch file from my Java Application?
(12 answers)
Closed 8 years ago.
Lets suppose I have a listener for a Button
public class Visualizer1 implements ActionListener {
public void actionPerformed(ActionEvent a) {
try {
Runtime rt2 = Runtime.getRuntime();
Process p = rt2.exec("visualizer/vis1.exe");
InputStream in = p.getInputStream();
OutputStream out = p.getOutputStream();
InputStream err = p.getErrorStream();
p.destroy();
} catch (Exception exc) {/* handle exception */
}
the "vis1.exe" will execute without any problem and it will open up
but if I have an application with a ".bat" extension like if it was(vis1.bat), it won't open up.
Note: .bat extension is an executable file
Try this..
Runtime.getRuntime().exec("cmd /c start vis1.bat");
a .bat isnt an executable file.
"A .BAT (short for "batch") file is a plain text file that contains a series of Windows commands. An .EXE (short for "executable") file is a binary file that contains much more complex executable binary code."
http://www.fileinfo.com/help/bat_vs_exe_files
Have you gone through previous threads on same issue on stackoverflow.com?
Have a look at followings:
How do I run a batch file from my Java Application?
Run batch file from Java code
How to execute a batch file from java?
Run a batch file with java program

How to set 777 permission on a folder structure in Linux from Java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do i programmatically change file permissions?
How can I set the umask from within java?
In Linux, I am trying to give full permission (777) to a folder structure (say ex: "home/test/sample/") for creating a MySQL table with changing the data directory to the user specific location from my application which is written in Java.
How can I give full permission to a folder structure from Java?
You have to use a Operating System dependent code just like this:
Runtime rt = Runtime.getRuntime();
Process proc;
int exitVal = -1;
try {
proc = rt.exec("chmod 777 "+file);
exitVal = proc.waitFor();
} catch (Exception e) {}

Categories