I wrote a shell execute program in java for php localhost:8080 with root directory /src/MyForm/ and file name is index.php. Directory Im passing from a string.
my code is like this.
String root = "/src/MyForm";
String[] command = {"php", "-S", "localhost:80 -t "+ root};
Process p = Runtime.getRuntime().exec(command);
im getting output like this
Not Found
The requested resource / was not found on this server.
enter image description here
What i'm missing ?
Related
I am using mysqldump like this:
Runtime.getRuntime().exec("mysqldump -u USERNAME -pPASSWORD DBNAME > /path/to/location/backup.sql");
in order to dump it into my local files, my java program is deployed using kubernetes.
Here is my code:
#RequestMapping(value = "/testDumping", method = {RequestMethod.POST, RequestMethod.GET})
public Object test(#RequestBody Map<String,Object> params) throws IOException {
String runStatement = (String)params.get("runStatement");
Runtime runtime = Runtime.getRuntime();
Process exec = runtime.exec(runStatement);
return exec;
}
And I finally got this exception "java.io.IOException: Cannot run program "mysqldump": error=2, No such file or directory". What is the problem here?
The error message is telling you exactly what the problem is:
java.io.IOException: Cannot run program "mysqldump": error=2, No such file or directory
This means that the shell is unable to find the executable file for "mysqldump". Try giving the entire absolute path to mysqldump inside your command like i.e. (assuming linux-like system):
Runtime.getRuntime().exec("/usr/bin/mysqldump -u USERNAME -p PASSWORD DBNAME > /path/to/location/backup.sql");
Exact path on Linux can usually be found using command which mysqldump
You could as well add this path to you PATH environment variable and keep your command as is (not 100% sure of this one as I'm usually working on Windows and am far from a Linux expert).
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.
I am trying to run this command in Python:
java JSHOP2.InternalDomain logistics
It works well when I run it in cmd.
I wrote this in Python:
args = ['java',
r"-classpath",
r".;./JSHOP2.jar;./antlr.jar",
r"JSHOP2.InternalDomain",
thisDir+"/logistics"
]
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
proc.communicate()
I have the jar files in the current directory.
but I got this error:
Error: Could not find or load main class JSHOP2.InternalDomain
Does anyone know what the problem is? can't it find the jar files?
You can't count on the current working directory always being the same when running your Python code. Explicitly set a working directory using the cwd argument:
proc = subprocess.Popen(args, stdout=subprocess.PIPE,
cwd='/directory/containing/jarfiles')
Alternatively, use absolute paths in your -classpath commandline argument. If that path is thisDir, then use that:
proc = subprocess.Popen(args, stdout=subprocess.PIPE,
cwd=thisDir)
I am trying to run a yarn job from a java wrapper program. The mapreduce jar takes two inputs:
A header file: I dont know the name of the file but the location and file extension and there's only one file at that location
A Input files directory
Apart from these I have an Output directory.
the processbuilder code looks like:
HEADER_PATH = INPUT_DIRECTORY+"/HEADER/*.tsv";
INPUT_FILES = INPUT_DIRECTORY+"/DATA/";
OUTPUT_DIRECTORY = OUTPUT_DIRECTORY+"/";
ProcessBuilder mapRProcessBuilder = new ProcessBuilder("yarn","jar",JAR_LOCATION,"-Dmapred.job.queue.name=name","-Dmapred.reduce.tasks=500",HEADER_PATH,INPUT_DIRECTORY,OUTPUT_DIRECTORY);
System.out.println(mapRProcessBuilder.command().toString());
Process mapRProcess = mapRProcessBuilder.start();
On run, I get the following error:
Exception in thread "main" java.io.FileNotFoundException: Requested
file /input/path/dir1/HEADER/*.tsv does not exist.
But when I run the same command as :
yarn jar jarfile.jar -Dmapred.job.queue.name=name -Dmapred.reduce.tasks=500 /input/path/dir1/HEADER/*.tsv /input/Dir /output/Dir/
It works all fine.
what can be the issue when running the command from java is causing this issue?
The * is being treated as part of the literal string in this case rather than a wildcard. Therefore globbing isn't expanding to your desired path name.
If there is only one file in the directory, why don't you find what its path is and pass that as the argument instead
eg.
File dir = new File(INPUT_DIRECTORY+"/HEADER);
if (dir.list().length > 0)
String HEADER_PATH = dir.list()[0].getAbsolutePath();
Using the code:
Process proc = Runtime.getRuntime().exec(new StringBuffer("cat ").append("../dir1").append("\"").append(File.separator).append("dir2").append(File.separator).append("hello text.xml").append("\""));
Fails with the error:
cat Error: cat: "../dir1/dir2/hello: No such file or directory
cat Error: cat: text.xml": No such file or directory
The actual file name is hello text.xml
You have to escape whitespace by using hello\ text.xml
Try with double quotes like "hello text.xml" . It worked for me.
I got a solution for the above Problem.
we can write the Command into a Shell Script file and execute the same through Process from Java Program and it's working fine. :)