I have to calling python script in java project.I dont use jython because in script i using todoist-api. I tried use this:
try {
String cmd = "/home/kiryushin/projects/python/stm/venv/lib/stmtest1.py";
Process p = Runtime.getRuntime().exec(cmd);
}
catch (IOException e){e.printStackTrace();}
I get
" java.io.IOException: Cannot run program "/home/kiryushin/projects/python/stm/venv/lib/stmtest1.py": error=13, Permisson denied"
I try change permission with chmod -r 777, and other commands but i get this error again.
Ubuntu 18.04 lts. JDK 10. Intelij idea community edition.
Try with :
First manually check with sudo permission.
And modify:
String cmd = "sudo python /home/kiryushin/projects/python/stm/venv/lib/stmtest1.py"
use your command like this, echo your password then use sudo -S along with your command
String cmd = 'echo your_password|sudo -S python /home/kiryushin/projects/python/stm/venv/lib/stmtest1.py';
That's how I solve my problem
Recreate my project without virtual environments (venv)
Add shebang in python script
With pip3 add lib todoist-app
In calling python script i write:
String cmd = "python3.6 /home/kiryushin/projects/python/stm2/stm.py"; Process p = Runtime.getRuntime().exec(cmd);
Related
I have tried POC for my project. I am run some cmd commands are through my java program without user interaction with the help of JavaRuntime. Now I can able to run the command, but not able to give some input on runtime.
This is my sample code.
Process process = null;
try {
Runtime runtime = Runtime.getRuntime();
process = runtime.exec(command);
logger.logOutput(process.getInputStream(), "Response Success : ");
logger.logOutput(process.getErrorStream(), "Error: ");
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
My sample Commands
"cmd /c start cmd.exe /K "E: && cd E:\Live\rsrk-node-api && git status\""
this above comment is executed perfectly.
My other command is: "git pull origin master"
now this command is needed my bitbucket password to access. I need to enter this password through the java program.
I have tried alternate way like configure ssh to my bitbucket but in the windows system every time asking password.
So please help me to resolve this problem.
I would try rather:
cmd /C "cd /d E:\Live\rsrk-node-api && git status"
Note the cd /d part which will ensure the drive is changed.
There should be no need for cmd ... start cmd...
Make sure your program is running with your account, and that git config credential.helper is set to manager.
If you are using SSH, try first with a SSH key without passphrase.
I need a multi command to bring the CMD to the root of the disc it runs on.
This is how the structure looks on the USB
\data
\data\commands
\Java
\Java\bin
App.jar
App.bat
This is how the "event" looks:
String command = "cmd /c cd\\data\\commands && wscript \"invisible.vbs\" \"Ready2Go.bat\"";
try {
Process p = Runtime.getRuntime().exec(command);
} catch (Exception e) {
e.printStackTrace();
}
This works perfekt when running App.jar file and "using" the java thats installd on the computer it self.
But when i try to run it with the java that i installd on the usb it will not work. (guide to install java on usb
app.bat file:
set Path=\Java\bin;%%Path%%
java -jar app.jar
So i need the a command to Leave the \java\bin dir. (for it is from there, I think the program is now running) and then run my command.
i have tried:
"cmd /c cd\\ && cd\\data\\commands && wscript \"invisible.vbs\" \"Ready2Go.bat\"
But without much luck.
I really hope you understand what I mean
write a startup batch start.bat like so
#echo off
rem change to this batch's folder
pushd "%~dp0"
set Path="%~dp0\Java\bin";%%Path%%
set JAVA_HOME=%~dp0\Java
java -jar App.jar
popd
and execute it as \data\commands\start.bat
Setting the current path of a process from java is done using ProcessBuilder and setting the directory-property. See a sample here
How to set working directory with ProcessBuilder
I'm trying to execute a linux command in my java code. It needs to change permissions for some directory.
Here is my attempt:
String Cmd = "echo myPassword | sudo -S chmod 777 -R /home/somePath";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(Cmd);
The command held in String Cmd is working perfectly when I used it in terminal. But when I use it in my code nothing happens. There is no error or warning feedback that helps me to understand my mistake. What might be the problem?
Java will not magically select bash as your executable. You probably want to do something like
"bash -c <your command>"
See this question:
How to run unix / shell commands with wildcards using Java?
(Also the | is a bash-thing. Java won't magically create pipes between processes.)
I have a sh file which conatins a many sudo commands, say a script as below:
test.sh
#!/bin/sh
sudo apt-get install gedit
sudo date --set="2014/02/20 10:00"
When I execute the test.sh from terminal it works fine. Problem is when I try to execute this file from a java class. The script doesn't execute. I tried this:
Process proc = Runtime.getRuntime().exec("/bin/sh","/home/priyatam/test.sh");
Does anyone have an idea to execute this sudo command from java class? Please share.
well as the script will be outputting the prompt to the screen, it will be necessary to capture the processes inputstream
InputStream stdout = process.getInputStream ();
I would also recommend using
ProcessBuilder builder = new ProcessBuilder("/home/user/test.sh");
builder.redirectErrorStream(true);
Process process = builder.start();
When I run commands from the console everything is OK:
sudo -u oracle fgrep ...
When I run the same command from Java code using ProcessBuilder, sudo doesn't work, and I need to set chmod to 775 or else I don't have permission to read logs.
Why doesn't this work? Is there an option to read logs without chmod 775?
Here is how I am using ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("bash", "-c", command);
Process shell = pb.start();
InputStream is = shell.getInputStream();
Since you say chmod 775 for log file it works, it's obvious your process doesn't have permission.
You can run your java with sudo:
sudo java ClassFileName
Or just add sudo as the first string in the array that you pass to bash process:
command[0]="sudo -u oracle ";
//command[1]=commandname;
//command[2...n]=Other params;
Assuming user oracle is in sudoers list and won't ask for password, this will run just like how it runs in commandline when you use sudo.
a. You don't need the bash -c, when you're executing the command you have a shell.
b. The command needs to be split on spaces and then passed into the ProcessBuilder as an array.