Java Programatically simulating chmod -R 755 [duplicate] - java

This question already has answers here:
How do I programmatically change file permissions?
(12 answers)
Closed 7 years ago.
I have a piece of code using which I create a new directory.
I want to do a chmod -R 755 ./destDirectory
Way I am creating files is:
File destFile = new File("/home/destFile");
File oneFile = new File("home/destFile/1");

Check out the API documentation for the java.no.file.Files class, which has a variety of utility methods for setting attributes, permissions, and performing other actions not found in the java.io.File class.
You can use the createDirectory(...) method to set permissions upon creation, or the setPosixFilePermissions(...) method for an existing file or directory.

Related

/system/bin/sh: no closing quote How to fix? [duplicate]

This question already has an answer here:
Command working in terminal, but "no closing quote" error when used Process.exec
(1 answer)
Closed 8 months ago.
This post was edited and submitted for review 8 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I have the next command that allows me to copy allure results (logs, screenshots of completed tests) from /data/data/com.example/files/allure-results folder (android 10, 11) to sdcard. (i need it because sdcard is private folder on device from android 10 and i use workaround with tar process that allows me to untar required folder to protected sdcard folder)
adb exec-out "run-as com.example sh -c 'cd /data/data/com.example/files && tar cf - allure-results' | tar xvf - -C /sdcard/
When i start it from local computer terminal everything is ok.
But for some reason, i have the next error if i execute this command from code via ProcessBuilder -> /system/bin/sh: no closing quote
command in code looks like:
exec("adb exec-out \"run-as com.example sh -c 'cd /data/data/com.example/files && tar cf - allure-results' | tar xvf - -C /sdcard/\"".split(" "))
How can fix it? Any ideas?
P.S. don't suggest to use TestStorage from androidx.test.services etc it doesn't fit my case and based on scratches for android < 10...
solution pass as array:
https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String%5B%5D)
I think the issue comes from the last "split()" method.
The result of splitting:
"adb exec-out \"run-as ...".split(" ")
is:
"adb", "exec-out", "\"run-as", ...
The third element in the array split() generated has a not closed ".
I would suggest to remove the .split() and to pass exec() an array of three elements.
"adb", "exec-out", "run-as ...The-rest..."

Java fails to create directory / file from the command line [duplicate]

This question already has an answer here:
Java error in making file through console
(1 answer)
Closed 4 years ago.
I have an executable that generates some file, and I need to call this executable from a Java application. The command goes like this
Generator.exe -outputfile="path/to/file" [some other params]
It works fine on the command prompt, but running it from Java,all steps are executed but the file is not created.
I doubt the problem was that my java application is not able to crate files / directories, so I tried to create a directory as below
try {
String envp[] = new String[1];
envp[0] = "PATH=" + System.getProperty("java.library.path");
Runtime.getRuntime().exec("mkdir path/to/folder", envp);
}
catch (Exception e) {
e.printStackTrace();
}
I get the following exception, even If the directory exist
java.io.IOException: Cannot run program "mkdir":
CreateProcess error=2, The system cannot find the file specified
I also tried using java.lang.Process and java.lang.Process and I got the same exception, although the command mkdir path/to/folder works fine on the command prompt
Two points:
1) You don't need to pass in the java.library.path to the mkdir command. Mkdir expects one parameter - the directory/ies you want to created.
2) Why not use the Java File class to create the directory instead? Create a File object of the path, then call the mkdirs() function on it.

Build .exe with javafx-gradle and InnoSetup and admin rights [duplicate]

This question already has answers here:
How to set 'Run as administrator' on a file using Inno Setup
(3 answers)
Closed 4 years ago.
I'm currently working on a project where I have to build a desktop application using JavaFX. I'm using the javafx-gradle-plugin (https://github.com/FibreFoX/javafx-gradle-plugin) for building and bundling the application.
Everything works fine, but after the installation the application doesn't request administrator privileges to run. If I start it with admin user everything works, but if you start it as a "normal" user the application doesn't work.
Is there a way for requesting admin privileges when starting the .exe?
thank you in advance for your help
I found the solution to my problem.
I added an entry in my .iss file for generating the InnoSetup installer like this:
[Registry]
Root: "HKLM"; Subkey: "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers\"; ValueType: String; ValueName: "{app}\xxx.exe"; ValueData: "RUNASADMIN"; Flags: uninsdeletekeyifempty uninsdeletevalue; MinVersion: 0,6.1
Be aware that when you add this entry you have to add runascurrentuser to your Run configuration like this:
[Run]
Filename: "{app}\xxx.exe"; Parameters: "-Xappcds:generatecache"; Check: returnFalse()
Filename: "{app}\xxx.exe"; Description: "{cm:LaunchProgram,xxx}"; Flags: runascurrentuser nowait postinstall skipifsilent; Check: returnTrue()
Filename: "{app}\xxx.exe"; Parameters: "-install -svcName ""xxx"" -svcDesc ""xxx"" -mainExe ""xxx.exe"" "; Check: returnFalse()

How do I rename a folder on my local machine using Java? [duplicate]

This question already has answers here:
Check if file is already open
(8 answers)
Closed 8 years ago.
I want to rename folders using Java 7. I have tried using:
srcDir.renameTo(desDir);
But from what I can understand (and personal experience) .renameTo only works for renaming files. I have also tired using:
FileUtils.moveDirectory(srcDir, desDir);
But I get this error:
Exception in thread "main" java.io.IOException: Unable to delete file: C:\webapps\37\WEB-INF\lib\stax-api-1.0-2.jar
at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:2279)
at org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:1653)
at org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:1535)
at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:2270)
at org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:1653)
at org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:1535)
at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:2270)
at org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:1653)
at org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:1535)
at org.apache.commons.io.FileUtils.moveDirectory(FileUtils.java:2756)
at batDel.batDelJavaC.main(batDelJavaC.java:52)
Note: I am running on a Tomcat 8 server, but have stopped it before running my project.
It is very likely that the .jar file in question is actually opened by some process like the application server. Until it closes the file, you won't be able to move or delete it.
On Windows (which it looks like you're using) you can use a utility like Process Explorer from SysInternals to see if some process has the file open.

Unable Create Directory in CentOS using Java [duplicate]

This question already has answers here:
Unable to create the directory error
(6 answers)
Closed 6 years ago.
I'm creating a Web Application using Java Servlets. I used JDK7, Tomcat7. I was perfectly working till I hosted local site in windows. But when I migrated to CentOS directory is Not creating.
File outputFile = new File("/Data/");
try{
if(!outputFile.exists()){
if(!outputFile.mkdir()){
throw new UnableToCreateFolderException();
}else{
outputFile = new File("/Data/" + user);
if(!outputFile.mkdir()){
throw new UnableToCreateFolderException();
}
}
}else{
outputFile = new File("/Data/" + user);
if(!outputFile.exists()){
if(!outputFile.mkdir()){
throw new UnableToCreateFolderException();
}
}
}
This is the code I used in Windows. And I tried to create "/Data" directory and give full access to user and then I tried to run it.
That path (/Data) is in the root directory, which the user you're running Tomcat as certainly shouldn't have write access to. It's always bad design to hard-code paths like this in your program; instead, you should read a property (such as myapp.homedir) and use that instead.
Just execute this command and try again,
chmod 755 /Data
chmod 777 /Data/*

Categories