Can't Change File Permissions from Within a Java Program - java

I have a Java program, that on runtime, extracts some executables to a specific folder, and tries to run them. Of course, before running the executable, its permissions need to be changed. For that purpose, I am using the following piece of code:
public static void changePermissions(String filename,String path){
String[] cmd=new String[3];
cmd[0]="chmod";
cmd[1]="u+x";
cmd[2]=filename;
BetterRunProcess process=new BetterRunProcess();
process.runProcessBuilderInDifferentDirectory(cmd,path,1,0,0,"");
}
In the above code snippet,the variable path contains the path to the executable, and filename is the name of the executable. The line:
process.runProcessBuilderInDifferentDirectory(cmd,path,1,0,0,"");
executes the command "chmod u+x ...". On my own computer, the code works just fine, but when I run it on someone else's computer, the following error is thrown:
chmod: changing permissions of deviceQuery.out. Operation not permitted.
Can someone figure-out what might be the problem behind this?
Here is some more code, that might be helpful.
public void runProcessBuilderInDifferentDirectory(String[] cmd,String path,int printToConsole,int printToExternalFile,int append,String fileName){
ProcessBuilder builder;
if(cmd.length==1) builder=new ProcessBuilder(cmd[0]);
else if(cmd.length==2) builder=new ProcessBuilder(cmd[0],cmd[1]);
else if(cmd.length==3) builder=new ProcessBuilder(cmd[0],cmd[1],cmd[2]);
else if(cmd.length==4) builder=new ProcessBuilder(cmd[0],cmd[1],cmd[2],cmd[3]);
else builder=new ProcessBuilder(cmd[0],cmd[1],cmd[2],cmd[3],cmd[4]);
builder.directory(new File(path));
try {
Process pr=builder.start();
if(printToConsole==1) printToConsole(pr);
if(printToExternalFile==1) printToExternalFile(pr,fileName,append);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Thanks!

Run your java code from that user which has the permission for that file.

Related

How to find TextEdit on Mac?

My Java app will detect file extension and open it in Windows using wordpad, like this :
public static Process Display_File(String File_Path)
{
String Command,Program,Suffix=File_Path.toLowerCase();
Process process=null;
if (Suffix.endsWith("txt") || Suffix.endsWith("json")) Program="C:\\Program Files (x86)\\Windows NT\\Accessories\\word_pad.exe ";
Command=Program+"\""+File_Path+"\"";
try { process=Runtime.getRuntime().exec(Command); }
catch (Exception e) { e.printStackTrace(); }
return process;
}
But it won't work on Mac, I know there is TextEdit.app on Mac, so how to change the above code to run it on Mac ?
After the change, it looks like this :
public static Process Display_File_On_Mac(String File_Path)
{
String Command,Program,Suffix=File_Path.toLowerCase();
Process process=null;
if (Suffix.endsWith("txt") || Suffix.endsWith("json")) Program="/Applications/TextEdit.app ";
Command=Program+"\""+File_Path+"\"";
try { process=Runtime.getRuntime().exec(Command); }
catch (Exception e) { e.printStackTrace(); }
return process;
}
But I got this error :
java.io.IOException: Cannot run program "/Applications/TextEdit.app": error=13, Permission denied
How to fix it ?
Starting macOS Catalina system applications moved to /System/Applicatyions folder: https://support.apple.com/HT210650
So, new path to TextEdit is /System/Applications/TextEdit.app/Contents/MacOS/TextEdit
On El-capitan, giving the below path worked for me:
Program="open /Applications/TextEdit.app/Contents/MacOS/TextEdit";
You can Navigate into the TextEdit.app folder from a terminal window and make sure you have the executable in the right place before trying it out.
Also, you need to change the setting of command like this:
Command = Program+ " "+File_Path;

Java - Printing to cmd

So my problem here is that I'm not too sure how to print a set of commands to cmd. I have a batch file that runs a Minecraft server, and I need to be able to run commands through the command prompt that shows up when I run the batch file, which will in turn perform commands to the server.
Here is my code so far:
package com.Kaelinator;
import java.io.IOException;
public class ServerManager {
public static void main(String[] args){
try {
System.out.println("Opening");
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("cmd /C start /min " + "C:/Users/Owner/Desktop/rekt/Run.bat");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//System.out.println("Closing");
//process.destroy();
} catch (IOException e){
e.printStackTrace();
}
}
}
Yes, I understand that all this does so far is open the batch file. :P I am expecting something like this that I need to add to my code:
process.InputStream(command1);
But I am certain that there is more to it, something along the lines of bufferedWriters or something like that...
Whenever I try to get answers from Google, the answers always have a whole load of extra code, or have something completely different about them.
Thanks!

I want to run a batch file present on my remote computer

I want to run a batch file present on my remote computer but I am getting an error that you don't have permission to access the share.
errorcode--: 0x80070035 the network Path was not found
Please help me out
Thanks in advance
public class Remotly {
public static void main(String arr[]) {
String cmd;
try {
Process r = Runtime.getRuntime().exec("cmd /c start \\\\xx.xx.xx.xx\\D:\\batch\\sas.bat");
} catch (Exception e) {
System.out.println("Execution error");
}
}
}
The UNC path is incorrect, it contains a drive letter.
\\\\xx.xx.xx.xx\\D:\...
Instead, the folder must be shared by a name
\\\\xx.xx.xx.xx\\DriveD\...
To use it, you must map the network drive before, e.g. via
net use * \\\\xx.xx.xx.xx\\DriveD <Password> /User:<Username>

Running a .cmd file through Java code

I am trying to run a .cmd program from Java. Doesn't run.
I'm using Runtime.exec as advised in some other posts.
public void mouseClicked(MouseEvent arg0) {
Runtime runtime = Runtime.getRuntime();
String path = "E:/Marvin/Marvin_Cleanup.CMD";
try {
runtime.exec(path);
} catch (Exception e1) {
e1.printStackTrace();
}
}
Im not familiar with windows executeables but using the process builder combined with that url should work fine.
http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
If you call further batches from your batch file you might need to use cmd /c prefix. https://superuser.com/questions/712279/commands-run-in-a-batch-file-only-when-writing-cmd-c-before
String path = "cmd /c E:/Marvin/Marvin_Cleanup.CMD";

Send multiple shell commands with app

Ok I'm learning how shell commands work, so I decided to develop a app to send the commands. This is what I got.
moveDirectory.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
try{
Process send = Runtime.getRunetime().exec(new String[] {"cd /sdcard/music/", "cp pic1 /sdcard/pic1"});
send.waitFor();
} catch (Exception ex){
String toast = null;
Log.i(toast, "Couldn't copy file", ex);
}
}
});
But it isn't working, the first command is working, but not the second one. What should I add to it?
Thanks
EDIT: forgot to add the send.waitFor(); line
Use normal command delimeter ;
moveDirectory.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
try{
Process send = Runtime.getRunetime().exec(new String[] {"cd /sdcard/music/ ; cp pic1 /sdcard/pic1"});
} catch (Exception ex){
String toast = null;
Log.i(toast, "Couldn't copy file", ex);
}
}
});
In this code you
1) go to the /sdcard/music
2) copy from /sdcard/music pic1 to /sdcard/pic1
I'm speculating, but you may have misunderstood what the parameter to exec really is. It's not a list of commands to be executed (effectivly a batch/shell script), but a single command WITH it's arguments to be executed by a shell. Making it a one-liner like Pepelac suggests or putting the series of commands into a single file that you execute with exec later may be what you are looking for. For the command you are trying to execute there is absolute no reason why you can not make it a one-liner with the full source path included (instead of changing to it), but there may be other reasons why you need to do this that you have not mentioned.

Categories