Can't run java from PHP's exec - java

I've been trying to run a JAR file that would read from input.txt and write to output.txt this way in console:
java -jar file.jar input.txt output.txt
And it works 100% fine on my machine. I need to run it inside a php script, and this code works 100% fine for me (Mac OS, php built-in server):
exec("java -jar file.jar input.txt output.txt");
But once I deploy it (CentOS server) where the exec function is allowed, it fails, it returns an empty string and the jar does not work, running it directly from shell is OK.
How can I fix that?
Thanks in advance!

The problem with your exec() is that PHP doesn't know where Java is on the server. Update your command to specify the full path to the Java executable and it should work, though you should also use full paths to the jar and text files while you're at it.

Related

run executeable jar with a linux script

I wrote a little java program and now I want to execute this .jar file from a .sh script.
my script:
#! /bin/bash
java -jar /var/spool/sms/sentSMS.jar
then i run the command: sudo bash sentSMS.sh
an get following error:
ERROR: Unable to access jarfile /var/spool/sms/sentSMS.jar
I am using a Raspberry with raspian-jessie, if this important to solve it.
Sorry, but I'm new in scripting with linux.
Take into account that the user must have at least READ permissions on that file.
Also, as you say you are new in linux, make sure the name is correct. sentSMS.jar is different from sentsms.jar

Trying to execute jar using cygwin, arguments not recognized

I have a makefile that calls a java parser to process some files before compilation. While in linux works perfectly when I am running that on a Windows 7 machine using cygwin my input are recognized as jar files.
My command inside my make is something like
java -jar ${MY_DIR}/myParser.jar -arg1 -arg2 -arg3 input.file output.file ;\
and I get an error that input.file is not a valid jar file... I assume it has something to do with my paths and how cygwin handles java but I can't make it work.
Thanks in advance
Try putting up ()
exec java -jar $(cygpath -w /path/to/myParser.jar) -arg1 -arg2 -arg3 input.file output.file
Srry not enough rep to add comment .

Connecting to MySQL database using Java in command prompt

Since I don't have netbeans right now, I am trying to connect to MySQL database from my Java code through command prompt. But it it is not taking the mysql-coonectivity.jar file.
Does anyone know any way to run my program??? Please help.
Try executing the program as it is windows OS
java -cp .;path\of\your\mysql-connector-java-5.1.6.jar className
In case you don't have the mysql-connector-java-[version].jar get it from here
You need to add your MySQL connector jar file while compiling and running your program, you can do it the following way,
To compile :
javac -g -cp mysql-coonectivity.jar; YourFileName.java
To Run
java -cp mysql-coonectivity.jar; YourMainClass
NOTE: The above written syntax assumes that your jar file is present at same location as of your program.
To execute program in Linux (Ubuntu) OS follow the below command format:
java -cp .:/usr/share/java/mysql-connector-java-8.0.25.jar MainProgram
Note:
Assuming you are in the directory where MainProgram.class is exist
After .(dot -- this is for current directory) there is :(colon) as a separator in Linux

Java -cp on linux

I made a program which runs fine on windows. When I moved it over to CentOS, I got this error:
Error: Could not find or load main class org.wbc.WBCController
This is the file setup and .sh on linux:
And this is the file setup and .bat on windows:
Does anybody know what the problem is, and how I can fix it?
Java will respond with this error even if it cannot find the file wbc.jar. I am guessing that that is your problem. You might want to see that your are executing the shell script from within the right working directory.
Check to see if you can run wbc.sh from the console or put this in wbc.sh to make sure it searches for the jar in the same directory as the shell script:
#!/bin/sh
java -cp `dirname $0`/wbc.jar org.wbc.WBCController

Run java class from single command using makefile

I have a homework assignment in Java that is tested using the commands:
make
./<program_name> <arguments>
my make file compiles my java program successfully, but how can the program be run without using the command:
java <program_name>
I have investigated how to convert a .jar into an .exe but I am convinced that is not the answer I am looking for.
I believe the test is run on a Linux machine. Is there something I can include in the make file to cause the command
./<program_name>
to run a compiled java class?
Without converting the java program in a native executable file, that will be different for linux, for windows and any other platform (so you will loose Java portability), the only thing you can do is to create a launch script.
On *nix system you can create a bash script and on windows a batch script. Then in this script you have to call java <program_name>.
With the script you are now able to launch your application with a single command.
For example on unix you can create myapp.sh:
#!/bin/bash
java -classpath bin com.test.YourApp $*
and make this script runnable with command
chmod a+x myapp.sh
in this example when you write myapp.sh command you launch your Java class com.test.YourApp using the folder bin as classpath.

Categories