I want to run -jar file from my php script, my -jar file run perfectly through commandline but i want to integrate it to my website but it is not working properly and also not provide me any error. my code is below
if (isset($_POST['Export'])) {
exec('java -Xmx1024M -jar C:\UploadTest-1_2.jar http://localhost:8080/packaging/Package C:\book\testbook.pdf –pass park345');
echo 'export button clicked';
}
output is:
export button clicked but -jar not doing needful.
Is Java on the path?
Try the command with "java -version", if you don't get output, Java is not on the PATH defined within the PHP environment.
Is the environment locked down?
When running PHP code, one generally doesn't allow any kind of execution to take place, as people could attempt to hijack the PHP environment to place code on the server which wasn't there. This means that you will have to take into account any type of chroot'ing, acl controls, permissions, selinux, and web server configurations which make it harder to run items without a clear knowledge (that means prior configuration) that the application should be allowed to run.
Related
I have a problem running the exec command from within a php script.
here's the detailed scenario:
I have an executable my_exe which runs from command line. This executable uses an environment variable, so I prepend the command with MY_VARIABLE='value' (works fine).
I use the exec command to run this executable from a Php script. ex: exec("MY_VARIABLE='value' my_exe"); (works fine too, tested in a browser)
The problem is in this part. When I call the php script from a Java program using HttpUrlConnection, I have two cases: if I run the Java program from outside the server (using public IP address) I get the good results, but when I run it from the server (using local IP, form command line ex. "java -jar /path/to/my/jar"), the Php works but the exec doesn't seem to be working properly. The environment variable is not being set in this case, so the executable runs but without the right values, so I get no results.
Any help is appreciated. I'm really stuck here. My guess is that there is a problem with Php or Java permissions, but I can't figure it out.
Strange command exec("MY_VARIABLE='value' my_exe");... it will work only on linux machine and will not on windows.
As a first step, try to rewrite you php code like this:
putenv("MY_VARIABLE=value");
exec("my_exe");
and see what happen.
The following process normally works for my startup scripts. However, when I introduce a command to execute a JAR file, it does not work. This script works while I am logged in. However, it does not work as a startup script.
In /etc/init.d I create a bash script (test.sh) with the following contents:
#!/bin/bash
pw=$(curl http://169.254.169.254/latest/meta-data/instance-id)
pwh=$(/usr/bin/java -jar PWH.jar $pw &)
echo $pwh > test.txt
Make script executable
In /etc/rc.local, I add the following line:
sh /etc/init.d/test.sh
Notes:
I make a reference to the script in /etc/rc.local, because this script needs to run last after all services have started.
Please do not ask me to change the process (i.e., create script in /etc/init.d/ and reference it from /etc/rc.local), because it works for my other startup scripts.
I have tried adding nohup in front of java command, and it still did not work.
Thanks
As written, there is insufficient information to say what is going wrong. There are too many possibilities to enumerate.
Try running those commands one at a time in an interactive shell. The java command is probably writing something to standard error, and that will give you some clues.
I executed the netsh command from the CMD that was manually opened by me by right clicking the CMD icon from the start and then selecting run as administrator from the options.It worked fine.Now I tried to run the netsh command through my java code,then it is not working.Nothing is happening when i run that code.I want to ask that I can run applications like notepad.exe from the cmd by calling appropriate methods of the runtime class from my java code,But how can I open the same application with the administrator priviliges from my java code.r.exec("notepad"); where r is an object reference to the runtime class will run the application,but the notepad so opened will not be in administrator mode.Actually I guess that learning to run the application in administrator mode from CMD will be enough to do the work done as The corresponding CMD command will be passed as the argument to the exec() method of the Runtime class.So my questions are:
How to run any application from CMD in windows 8 with administrator privilliges?
The way i want to implement the use of netsh is a good thing to practise or there is some other way out i must use these commands from my java code.
I have seen some commands while googling but they where not working out for me,like runas /user:administrator "notepad.exe" etc.
Thanks
You cannot use the runas /user:administrator approach, as that requires a password input which you cannot provide from an external source (such as a Java application) for security reasons.
I had a similar issue to you in the past, and I solved it using PsExec, running the process on localhost with an administrator username and password allowed me to execute external applications as an administrator.
Using your example you could run:
PsExec.exe \\\\127.0.0.1 /accepteula -u USER -p PASSWORD notepad.exe
The "/accepteula" flag prevents the requirement to accept the EULA interactively when run on a machine for the first time.
This approach may require a bit of tweaking to get working with your setup, but hope it gives you a starting point.
I'm trying to build a script that starts minecraft_server.jar (location: user directory / mineserver / minecraft_server.jar ).
I have PHP and Apache installed, and I'm trying trying to start the server JAR from /var/www/html/interface.php.
Via console, the server starts fine by running:
java -Xmx1024M -Xms1024M -jar mineserver/minecraft_server.jar nogui
... from the user directory. So in the interface.php file I have the following (note location listed above):
system('java -Xmx1024M -Xms1024M -jar /home/ec2-user/mineserver/minecraft_server.jar nogui', $retval);
But the server never starts after I visit the file. What am I doing wrong?
Thanks for any and all clues.
I want to say thank you for your help on Thanksgiving. While I haven't found the solution yet, I appreciate the efforts. Thanks again.
Have you checked the error log? It's at /var/log/httpd by default. If anything went wrong, it should be there. Otherwise, it could be starting wrong and I would recommend launching it in a screen so you can hop in and check on anything it's doing.
Install screen through whatever method your distribution uses, and then change the launch string to be:
screen -dmS "minecraft" java -Xmx1024M -Xms1024M -jar /home/ec2-user/mineserver/minecraft_server.jar nogui
The command will be executed with the permissions, and the environment of whatever user account the web server runs under.
In particular, it may have a different PATH entry, and the java command might not even be found, or it might be found somewhere else etc...
Also, be aware of what current working directory it is inheriting from your PHP script.
See exec() and check the output to help yourself do some basic debugging.
Also, you probably want something more like
exec('nohup java etc... > /dev/null 2>&1 &');
so that the process gets properly backgrounded and disconnected from the webserver parent process.
I am running a shell script through a web application. This shell script looks something like
`#! /bin/bash
user=""
pass=""
db_url=""
db_instance=""
sqlplus -s $user/$pass#$db_url/$db_instance # ./SqlScripts/foo.sql
sqlplus -s $user/$pass#$db_url/$db_instance # ./SqlScripts/bar.sql
CLASS_PATH="./lib/*"
java -classpath $CLASS_PATH package.Main ./Data/inputfile`
I am using ProcessBuilder to run the script and everything but the last line works fine. Am I creating a problem by calling shell through the jvm then calling the jvm again to run the application?
The problem was the environment that the script execution process was running in. I changed some of the environment variables of the process and everything is working fine now. The script was initially a standalone shell script, but I wrote one script for each of the databases used. In order to control the workflow I wrote a web application for this which calls seperate threads for each script and can manage the threads. Thanks for the responses!
Often, app servers run their servlets in a 'clean room' environment - e.g. they strip away all the variables that would normally be set from the outside for security reasons. Try using a fully qualified path to the java binary, and also try setting a full/absolute path for your CLASS_PATH variable.
The parent JVM and the child JVM should be separate processes, no particular reason why they should interfere.
What error do you get?
is java on your PATH?
OK, adding more questions in response to your comments ...
Which thread is waiting? Presumably the parent?
The child java process, do you have any evidence as to whether is succesfully initalises. My guess woukld be that the child is in some way blocked. If you kill the child does the parent then come back to life?
Suppose it was a simple "hello world" application, would that work?
Most likely the line:
CLASS_PATH="./lib/*"
And
$CLASS_PATH
It won't be expanded by the process builder because that's usually shells' job, which in this situation is not being invoked.
Try creating the complete list of ./lib/* and append it directly into the last line of your script.
java -classpath ./lib/a.jar:./lib/b.jar
Side note:
Invoking all this from java looks just bad to me. I would rather have it in a standalone script and invoke it by other means, but that's me.