I've been trying to use PHP to execute a shell script(.sh) and do Java saxon.transform to create a file. When I run the PHP file, it doesn't show any errors but the file is not being created.
script.sh is executable(chmod +x), and jar path is predefined.
script.sh
echo "Running step 01."
java net.sf.saxon.Transform -o:review.html -s:merged.html -xsl:xsls/01-simplify.xsl --suppressXsltNamespaceCheck:on;
if [ -f "review.html" ]
then
echo "Review.html file successfully generated."
echo "Done"
else
echo "Something went wrong."
echo "Check the <code>merged.html</code> file."
fi
php
echo shell_exec('./script.sh');
Result
Something went wrong.
Check the merged.html file.
The same java command works as it's supposed to when executed from command line. It just doesn't work on web.
I'm on Ubuntu 16.04, with PHP7.
Any suggestions would be appreciated.
Thanks.
If you check your error.log file it should give you more details as to what is going wrong. using the command:
tail /var/log/apache2/error.log -n 100
Or similar
I tried running a similar shell script within a php environment. I get the following error:
Error on line 7 of test.xsl:
Failed to create output file file:/var/www/html/samples/review.html: Permission denied
in built-in template rule
Failed to create output file file:/var/www/html/samples/review.html
It is probably permission problems you are getting when you create the output file.
Related
I am executing wkhtmltopdf command line tool from Java but it is throwing below error.
Cannot run program "wkhtmltopdf": error=2, No such file or directory
But note that when I execute this command line tool from my mac terminal then pdf got generated successfully. Please see below.
MacBook-Air-2:~ inDiscover$ wkhtmltopdf /var/folders/7y/2vr28n113p908ksnk0fnpqch0000gn/T/test7896850081571855407.html /Users/mymac/Documents/Project/emailbody/test2.pdf
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
I have seen lot many similar questions here (for ex: wkhtmltopdf: No such file or directory [ Closed ]) but issue with those questions are related to $PATH. In my case I believe that I have set the path to the executable to $PATH correctly. Please see below.
MacBook-Air-2:~ inDiscover$ locate wkhtmltopdf
/private/var/db/receipts/org.wkhtmltopdf.wkhtmltox.bom
/private/var/db/receipts/org.wkhtmltopdf.wkhtmltox.plist
/usr/local/bin/wkhtmltopdf
/usr/local/share/man/man1/wkhtmltopdf.1.gz
You can see here wkhtmltopdf has been added to $PATH (/usr/local/bin)
Also , see below the response for echo $PATH.
MacBook-Air-2:~ inDiscover$ echo $PATH
/Users/mymac/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
MacBook-Air-2:~ inDiscover$
I am getting this issue only when I try to execute the command from Java. Please see my java code below.
String VIEWPORT_SIZE = "2480x3508";
int CONVERSION_DPI = 300;
int IMAGE_QUALITY = 100;
List<String> cmd = new ArrayList<String>(Arrays.asList("wkhtmltopdf",
"--viewport-size", VIEWPORT_SIZE,
"--enable-local-file-access",
// "--disable-smart-shrinking",
"--dpi", String.valueOf(CONVERSION_DPI),
"--image-quality", String.valueOf(IMAGE_QUALITY)));
//cmd.addAll(extParams);
cmd.add("/var/folders/7y/2vr28n113p908ksnk0fnpqch0000gn/T/test7896850081571855407.html");
cmd.add("/Users/mymac/Documents/Project/emailbody/test3.pdf");
try {
ProcessBuilder pb = new ProcessBuilder(cmd);
if (Logger.level.compareTo(LogLevel.Info) >= 0) {
pb.inheritIO();
}
Process p = pb.start();
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
Am I really missing something?
Here are a few things you can check, one will hopefully resolve your issue.
Nearly all occasions I've seen where java is not finding an application that your shell or terminal does run are down to use of commands built into the shell / terminal (such as an alias), or because of PATH used in a script that you use to launch your java yourapp.ClassName ... is different to that set for the interactive shell / terminal.
The interactive shell / terminal may include or source from other scripts on startup - for example it may have run code in ~/.bashrc, ~/.login, ~/.profile, ... meaning that PATH declared inside interactive shell is not same as PATH presented to Java app when you launched it.
Hence you see terminal show PATH has /usr/local/bin:
MacBook-Air-2:~ inDiscover$ echo $PATH
/Users/mymac/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
But Java says no /usr/local/bin in PATH:
System.out.println("PATH="+System.getenv("PATH"));
/usr/bin:/usr/sbin:/sbin
So to fix your problem you could make java use absolute path to run the app wkhtmltopdf:
Arrays.asList("wkhtmltopdf" -> Arrays.asList("/usr/local/bin/wkhtmltopdf" ...
OR you could make java launch your shell / terminal so the terminal sources its env scripts as normal and runs the command:
# Confirm which shell your Terminal uses:
echo $SHELL
If you have say SHELL=/bin/bash you can run /bin/bash from java and let it work out where wkhtmltopdf is:
Arrays.asList("wkhtmltopdf" -> Arrays.asList("/bin/bash", "-c", "wkhtmltopdf" ...
OR if you have a script say runMyApp.sh to launch your java app, set the PATH before java yourclass.Name,
export PATH=/usr/local/bin:$PATH
java yourclass.Name
OR if you have a script say runMyApp.sh to launch your java app, make that it sources the same profile environment as your Terminal does. This depends on the SHELL but for some systems could be something like:
#!/bin/bash
# Load env from current user - "source" and "." may or may not work in the SHELL you are using:
source ~/.bashrc
# OR maybe other shell
. ~/.somefilerc
echo $PATH # Now hopefully includes same /usr/local/bin
java yourclass.Name
Try to add sudo in the front of wkhtmltopdf.
I am trying to execute a .jar file from a PHP script using the exec() command, more exactly using:
<?php
$command = 'java -jar myfile.jar';
exec( $command, $output);
?>
both the php script, and the jar are in the same directory on the server. The myfile.jar produces an excel spreadsheet, and I want this spreadsheet to be created or stored in the same directory or another directory for that matter on the server. When I execute myfile.jar in the terminal of my pc, the program works fine and outputs the file to the directory where the .jar is located.
Any ideas?
Best Regards.
If I understand correctly, you want to execute the myfile.jar file, which returns some data and save it to a file? If you don't need to edit the data, you can use:
exec ( 'java -jar myfile.jar > output.xml' );
Ok, so I installed PHP on my windows, and am using EasyPHP to test as if I had the server on my own computer.
The php code is working fine, but from the $output variable I could notice that the program suddenly gave a Fatal Error.
I managed to dechipher that this error was from the time limit PHP has for running scripts.
I used set_time_limit(0); and corrected this. Now the java program finishes its run, and no problem there.
However, the excel file is being created and comes out empty. I suspect this has something to do with the FileOutputStream that the java code uses to generate the spreadsheet.
Can you point me in the right direction ?
I'm rank new to bash thus the question.
I've a java program that I've exported as a .jar.
This file when run as
java -jar somefile.jar
goes into an infinite loop and awaits a file name. Based on the correct file path it generates an output.
How do I write a bash script to do automated testing of this project.
I need the scrip to do the following -
Run the program, which is run the same command
provide an array of 5 files as an input to the program
For each file write the output to an log file.
This should do it.
#!/bin/bash
files="$#"
for i in $files;
do
echo "Doing $i"
java -jar somefile.jar <<< "$i"
done
Make sure you chmod u+x filename it first. Then call it like this:
./filename firstfile secondfile thirdfile etc.
Other:
As sjsam pointed out, the use of <<< is a strictly bash thing. You are apparently using bash ("I'm rank new to bash..."), but if you weren't, this would not work.
Suppose my java program is HelloWorld.java. We can run it in 2 ways:
1st using executable jar
2nd by running java class from terminal
create a new text file and name it hello.sh
In hello.sh
!/bin/bash
clear
java -jar HelloWorld.jar
Save it and open terminal:
1 navigate to directory where your HelloWorld.jar is present
2 give permission to terminal to run the bash script by using the following command
sudo chmod 754 hello.sh
3 run you script by running the following command
./hello.sh
I am trying to execute ls command through busybox.
I am creating a .bat file to execute this command which i am calling through .java
However, i am not able to execute commands one after another in .bat file.
This is the contents of my .bat file
"C:\Documents and Settings\Some Directory\Android\android-sdk\platform-tools\adb.exe" shell
/data/busybox/busybox ls
what i think that once i start the shell through the first line of my .bat, the control from the shell is lost hence second command is not executed.
Because if i write my .bat file as
"C:\Documents and Settings\Some Directory\Android\android-sdk\platform-tools\adb.exe" shell ls
it works fine.
I need to write commands in my .bat file so that they exceute one after the another.
I have tried using CALL before each commands in .bat, still it does not work.
I have tried to use multiple .bat, still a fail cause.
Can someone please help me on this?
Thanks a ton.
I am unable to test this myself with ADB at this moment, but this works for other programs that have an input buffer. I will try to verify this tonight, but if anyone else confirms it before then, leave a comment.
#echo off
( echo shell
echo /data/busybox/busybox ls
) | adb.exe
One of my friend told me that he used a batch program to install a java program on the machine,that placed the necessary files in a particular directory and also planted a shortcut on the desktop. How can it be done ? If there are tutorials that teach this please link me to them
All you need to do is use some basic windows commands to make this work. I'm not going to write the script for you but I can point you in the right direction. A batch script on windows is a simple text file ending with the .bat extension.
You can use any command typically available on the windows command prompt (AKA cmd.exe). A good starting point is learning how to move and copy files around so you want to take a look at the commands by the same name on the Command-line reference from Microsoft. There is also a handy guide linked on the same page to batch files and how they work.
The documentation linked is for Widows XP and the syntax of the commands should be back and forwards compatible with other Windows versions.
Installing a Java program is the same thing as installing a ... program ;-)
You can create a batch installer from scratch with a .bat file or use an installer builder tool.
I use NSIS because it's free and simple to use ... but there's others.
You also may want to build an .exe instead of a jar file (sometime, windows open the jar archives instead of launching java). I use Launch4J to wrap my java application in a .exe file.
If the app. has a GUI, install/launch it using Java Web Start. It works on Windows, OS X & *nix, and can install both desktop shortcuts and menu items to launch the app. on platforms that support such things.
JWS is supported & supplied by Oracle.
This code is a simple batch script. Customize this code.
Code:
#echo off
color f0
:: overwrite your program name after the '=' ::
set ProgramNameHere=ProgramNameHere
goto start
:start
cd/
cd users
cd %username%
cd desktop
md %ProgramNameHere%
:: overwrite your file path on the 'DATA' ::
:: overwrite your file name on the 'file1', 'file2'...
:: overwritw your file name after the 'extracting'.
echo DATA>>C:\Users\%Username%\Desktop\%programNameHere%\file1.txt
echo extracting file 1
ping localhost>nul
echo DATA>>C:\Users\%Username%\Desktop\%programNameHere%\file2.txt
echo extracting file 2
ping localhost>nul
echo DATA>>C:\Users\%Username%\Desktop\%programNameHere%\file3.txt
echo extracting file 3
ping localhost>nul
echo DATA>>C:\Users\%Username%\Desktop\%programNameHere%\file4.txt
echo extracting file 4
ping localhost>nul
echo DATA>>C:\Users\%Username%\Desktop\%programNameHere%\file5.txt
echo extracting file 5
ping localhost>nul
echo DATA>>C:\Users\%Username%\Desktop\%programNameHere%\file6.txt
echo extracting file 6
ping localhost>nul
echo DATA>>C:\Users\%Username%\Desktop\%programNameHere%\file7.txt
echo extracting file 7
ping localhost>nul
echo DATA>>C:\Users\%Username%\Desktop\%programNameHere%\file8.txt
echo extracting file 8
ping localhost>nul
echo DATA>>C:\Users\%Username%\Desktop\%programNameHere%\file9.txt
echo extracting file 9
ping localhost>nul
echo DATA>>C:\Users\%Username%\Desktop\%programNameHere%\file10.txt
echo extracting file 10
ping localhost>nul
goto exit
:exit
exit