Having trouble with Tomcat running a python script - java

I am trying to run a python script from a java bean on a Tomcat server. The code that executes this looks like this:
Process child = Runtime.getRuntime().exec(test);
BufferedReader in = new BufferedReader(new InputStreamReader(child.getInputStream()));
String line = in.readLine();
String response = "";
while(line != null){
response += line;
line += in.readLine();
}
child.waitFor();
The error I receive is in the form of two pop-ups one that reads "Could not determine the package or source package name." and the other is an OS error from Ubuntu stating an internal error has occurred with the python script.
Is there something in Tomcat that needs to be setup in order for it to be able to execute a python script on the local system?
Some background, I need the python script to dynamically generate an image based on data selected by the user. My test system is Ubuntu 12.04, I will be deploying on a CentOS system.

Related

Issue executing a java compiled file from java source

I am trying to execute a java compiled file from another java program, and I am having some issues.
When I run from my terminal the command java -cp ".:lib/MyLib.jar" javaFiles/g1/MyCompiledProgram I can execute MyCompiledProgram without any issue.
But when I try to execute the same command from code using the following method:
Process process = Runtime.getRuntime().exec(String.format("java -cp \".:%s\" javaFiles/g1/MyCompiledProgram",Path.of(PATH_MYLIB)));
String error = null;
process.waitFor();
if(process.exitValue() != 0){
try(Scanner scanner = new Scanner(process.getErrorStream())){
error = scanner.useDelimiter("\\A").next();
}
System.out.println(error);
}
I get a ClassNotFoundException error. I checked that the directory java was using to execute the command was correct (running the pwd command) and it is the correct one.
Does anyone has any idea why is not finding the class? Thanks :)

Java Spring Boot failing to find Python Script in resources folder

I am trying to call python Scripts(in resources) from my Java class.
This is my code spinnet
String res = "/Scripts/Brokers/__init__.py";
URL pathUrl = this.getClass().getResource(res);
String path = "";
if(pathUrl != null)
path = pathUrl.toString();
ProcessBuilder pb = new ProcessBuilder("/usr/bin/python3.6", path);
ProcessBuilder is giving error No such file or directory.
P.S.
value of path = "file:/home/path-to-project/project-name/out/production/resources/Scripts/Brokers/\__init__.py"
Also how to include python scripts in jar file to run the project on linux server.
I am stuck with this issue since last 3 days. Please suggest.
From java doc:
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
Starting a new process which uses the default working directory and
environment is easy:
Process p = new ProcessBuilder("myCommand", "myArg").start();
So basically, you going to fork a sub process that will
start a python interpreter
with the python script that you have provided as argument.
The path to your script should be a normal path that your OS can understand, therefore you should not give a URI? like path (protocol:address/path).
if (path.contains(":"))
path = (path.split(":"))[1];
Also the backslash \ before __init__.py looks suspicious.
You should be able to run ls /home/path-to-project/project-name/out/production/resources/Scripts/Brokers/__init__.py and see the file, the same goes for ls /usr/bin/python3.6.

Run linux command in remote machine from java

I´m working with application in java.
I can execute linux command (bash) on my machine host, but i want to execute this command in a remote machine like ssh.
I´m ussing this code
Process process = Runtime.getRuntime().exec(script);
process = Runtime.getRuntime().exec(script);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
How i can execute linux shell in remote machine with java code?
Luis: as Eric suggested one possible solution is to run a local script that performs an SSH itself on the remote server.
For instance if you have a Linux->Linux environment, your script you could have something like:
ssh remoteuser#remotehost 'bash -s' < localscripttoexecuteremotely.sh
In a Windows->Linux scenario you could do:
plink remoteuser#remotehost -m localscripttoexecuteremotely.sh
Take a look at this thread for additional information.

exec not working with java 1.7.21, but in netbeans works fine

I made a little program and it worked fine, but now. First, it mux the xml chapter file in the mkv file, so we get a muxed mkv file. Some day ago I updated java to 1.7.21 and I think this is the problem why it is not working now. It's a little strange, but when I run in netbeans everything is fine, but when I build and I run the .jar file, it is not working. It create the xml file, but not mux in the mkv file (and because not muxed not delete the xml file). Here is the code: (filename=xml file path; mkv=mkv file path)
public void muxing() {
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("c:\\Program Files\\MKVtoolnix\\mkvpropedit.exe --chapters \""+filename+"\" \""+mkv+"\"");
if (p.waitFor()==0) {
File xmlfile=new File(filename);
xmlfile.delete();
}
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
The program worked with java 1.6 and I think with 1.7.17 too. Win7 32bit. Sorry for my bad English.
Oracle has made breaking changes to Runtime.exec() in Java 7 update 21 (and 6 update 45).
If the program name contains spaces, you need to specify command and arguments in an array:
Process p = Runtime.getRuntime().exec(new String[] {
"C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\""});
Another option is to use java.lang.ProcessBuilder:
Process p = new ProcessBuilder("C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\"").start();
As stated by Oracle:
Applications that need to launch programs with spaces in the program name should consider using the variants of Runtime.exec that allow the command and arguments to be specified in an array.
Alternatively, the preferred way to create operating systems processes since JDK 5.0 is using java.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.

tomcat java php

I want to use tomcat tomcat, java & php altogether. my java program creates a text file and write something on the file, when I run that not file is getting created. java program is running through command line. I am keeping all files in /usr/local/tomcat/webapps/testjava.
Content of Java File:
public class test
{
public static void main(String args[]) throws IOException
{
try{
Writer fos = null;
File outputFile = new File("outList.txt");
fos = new BufferedWriter(new FileWriter(outputFile));
fos.write("hi");
fos.write("\n");
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Content of PHP file:
<?php
$output = exec("java test", $ret);
echo $ret;
echo file_get_contents("outList.txt");
echo "HI";
?>
$ret given output as Array. HI is getting displayed.
One More Thing when I do php index.php on command line it is working, mean outList.txt is getting created.
var_dump($ret) is giving :
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.lang.RuntimeException: PHP Parse error: syntax error, unexpected T_ECHO in /usr/local/tomcat/webapps/testjava/index.php on line 7
php.java.servlet.fastcgi.FastCGIServlet.handle(FastCGIServlet.java:499)
php.java.servlet.fastcgi.FastCGIServlet.doGet(FastCGIServlet.java:521)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.RuntimeException: PHP Parse error: syntax error, unexpected T_ECHO in /usr/local/tomcat/webapps/testjava/index.php on line 7
php.java.servlet.fastcgi.FastCGIServlet.parseBody(FastCGIServlet.java:409)
php.java.servlet.fastcgi.FastCGIServlet.execute(FastCGIServlet.java:433)
php.java.servlet.fastcgi.FastCGIServlet.handle(FastCGIServlet.java:481)
php.java.servlet.fastcgi.FastCGIServlet.doGet(FastCGIServlet.java:521)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.12 logs.
Well, for starters your filenames are different between the Java and PHP code (outList.txt vs out.txt). Is this an error when posting the question or a real error in code?
Otherwise, I'd encourage you to have a look at the return value and error output (if any) of the PHP exec call - it's likely that the Java invocation is failing to run correctly and inspecting these will tell you why. There could be many reasons - java not found on the path for the user running the PHP preprocessor, the test.class file not found in whatever the classpath is set up as, etc. Being able to run the Java command interactively, and having it run by the webserver, are very different things as this is highly environment-dependent.
does your php works?
try running: echo "xxx";
in some php file
I don't know what is on line 7 of index.php, but that's where the problem seems to be. It's probably the line where you exec your java file. Most likely reason I can think of is that the server does not have permission to execute the java test app, or the app does not have proper permissions to create the text file.
The reason it does work when you run the php script from the command prompt is that you're probably the owner of the files and folders and when you run the php script it executes the java app with your userid and permissions. When you call the script via your browser it gets executed with the tomcat userid and permissions, which probably do not allow creating new files.
As a quick test you could make the folder in which the file should be created world writable by setting the permissions to 777. If it then works, you may want to look into a safer setting.

Categories

Resources