PHP exec() concurrency

Hi,

Let’s say I have a .JAR file that simply performs a calculation. I would like users of my website to be able to get the result of this calculation by invoking this JAR file via the exec command in PHP.

This works fine on local host using XAMPP, however I am unsure how this would work in a real deployment on a web server.

Providing the hosting provider allows the exec command and the invocation of java files, how will this work regarding multiple users? Is there a new thread created on the server for each invocation of the single JAR file?

If there are multiple users on my website and they all select the option that sends the PHP command to the server, will each user invoke a new instance of the JAR file? Is this something I have to deal with myself, or is this handled by the hosting provider?

To be clear, I have a small .JAR file that performs a unique calculation and returns the result. I want users of my website to be able to generate a result from this file and have it displayed.

Any help would be much appreciated

Thanks

Can not really be done. At least not with a normal web server. JAR is a Java file, not a Javascript file. Therefore it would need the full Java system to be installed on your server. I am guessing that you have Java installed on your computer. So your current setup is not a Java server. Is your live server owned by you? You could use Glassfish or Sun-Java to create a Java web server and then, you would have Java available. Do you understand this difference? If you own the server, you can set it up as a Java web server instead of a standard one. This is not possible on shared servers.

Thanks for your reply ErnieAlex!

So my problem isn’t really a concurrency problem, its a Java environment problem.

This is a disappointment. I didn’t realise that most hosting providers don’t have Java available. Since I have been experimenting on my local machine with JRE installed, everything works fine. No it’s not my own server, it’s shared.

I am not looking to display the application in the web page, it’s just a case of running the JAR, which is basically a console app, that simply returns a string to the user.

Do you think a Virtual Private Server would help? If I created a virtual server and installed the JVM, would this allow me to run the JAR?

You’re certainly going to be better of using a VPS (And, if you have no preference already I recommend DigitalOcean.

In there you’ll be able to install anything you like.

Just be carefull when using the exec function… any direct calls to the OS could be a source for command injection attacks

Thanks Mchojrin very much for responding!

I figured a VPS would do the job, but would have rather not moved my current hosting. The exec function will only call one single JAR file, and the user has no input on what file gets called, but I agree, security must be tight regarding the exec() call.

Would you say it makes sense to set up a VPS just to host the JAR file, in which it is invoked from my current website (from my current hosting provider), or would it make more sense to fully migrate my website to the VPS?

Also, if I did go the VPS route, how would concurrency come into it? Would I have to deal with it myself, or would each invocation of the JAR file be in a thread of its own?

Well, why not just convert the Java into PHP or C? There are converters out there that let you convert code from Java. Or you could hire a programmer here on this site to convert it for you. What math does this math process actually do?

Virtual means shared. So, it is not possible to install Java on a PHP web server if it is a virtual/shared version. You could upgrade your server to a dedicated server and then get full control of it, but, would cost a lot more. I did find one Java add-on in Github to allow Java calls to be made. It was called Javabridge. But, again, it needs Tomcat-Java installed. Kinda going in circles here. Keeping your current server mean it can not run Java.

Is the JAR file large? How many lines of code are we talking about? Conversion to C or PHP would be the simplest way to go in my humble opinion… Or, build your own server. They are cheap now and easy to set up. All you really need is a computer with a backup power supply and a dedicated IP from your ISP. Then, you could set up whatever you want on it… Just another thought.

Thanks again for your reply.

It is not a math calculations exactly. It uses Apache POI to open and manipulate a MS Excel file. When I input a number in cell A1, a formula in cell A2 is evaluated. The JAR file opens a particular Excel file, inserts a value in cell A1, and retrieves the value in cell A2.

This is the whole project in a nutshell. Ideally I would like to be able to invoke macros too, but feel this may not be possible. I have just come across PHPExcel/PHPOffice which looks like it may help me get around using Java.

I will continue to look into this.
Thank you all for contributing to this thread.

Well, first, Excel is on your local machine. It will NOT run on your web server. So, this Java issue is useless for a real server.

Now, with that said… There are very good libraries that run on any web server to read Excel sheets. Read and write them. But, none that I am aware of to actually run them. Therefore the A2 results can not be calculated. BUT, a solution would be to create a web-version of the Excel sheet. Simple, super simple to do. Just load up your Excel, save-as-webpage. Done. You would need to make a slightly odd way to alter the A1 and reload the page and then read out A2. Since the Excel-Web-Page would be a PHP or ASP page, you would have to know where the field is stored, but, should not be too hard to do that. Just another thought.
Macros are just sequences of commands and could be changed to PHP with ease. You can access A1,A2, etc thru the use of a library to read or write them. One Excel library that can read/write Excel sheets is PHPexcel which I have used and it is easy to learn. It can not RUN the sheet but can read/write it. Just more info for you…

ErnieAlex, you have been a great source of information, thanks again.

PHPExcel seems to have been permanently archived in 2019 and is discontinued.
I am not sure how I would edit the Excel file once it has been saved as a web page.

I’m very concerned about editing an Excel file on a web server, like you say, the Excel file will not be run on the server. I think I may need to redesign my application. I would have preferred to keep using excel, as there are a number of complex formula in there and it would be quite complex to transfer all this to PHP.

Either way, you have shed light on this problem, and I am very grateful for your time.

No problem! This is why we are here! There are some converters and I saw a couple that converts from Excel to PHP, but, not sure if they really work.

To redesign the system, the question is who fills in the data in the Excel sheet. You can convert the VBA code in the Excel, meaning the formulas and keep the data in a database. But, think it all out and come back if you have further questions. Good luck…

If you can host everything in the same server I believe it will be easier so… I’d try to fully migrate.

I’m not sure about this, but I guess it would run in a separate thread.

There’s a new version called PhpSpreadSheet. I use it all the time. It’s really good

Sponsor our Newsletter | Privacy Policy | Terms of Service