I've tried with:
ProcessBuilder pb = new ProcessBuilder("osascript script.scpt");
pb.inheritIO();
pb.directory(new File("bin"));
try {
pb.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but I always get the error "no such file or directory".
I've tried also with:
Runtime.getRuntime().exec("osascript script.scpt");
but nothing happens.
I also tried using this string in both the snippets above, but nothing changed.
osascript -e 'tell application \"Safari\" to quit'
I was able to fix the issue by using this
ProcessBuilder proc = new ProcessBuilder("osascript", "script.scpt");
This is because there's no tokenisation: the command to run is assumed to have already been tokenised.
Related
I have python code. I run it using python -m base-package arguments
I want to run this project from a spring boot project and use the output given by the python. I could not find any simple tutorial, is there any simple way to get this done?
Try execute your command with this:
private String getResponseFromCommand(String command) throws IOException {
Process process = exec(command);
InputStream in = process.getInputStream();
String response = new BufferedReader(
new InputStreamReader(in))
.lines().collect(Collectors.joining("\n"));
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
I wrote this for running docker commands and getting feedback from them. It should work for any command though.
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.
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;
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 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";