Applet executing program on server - java

I've got an applet, which has to exec a program on the same server.
Runtime c = Runtime.getRuntime();
window.finishedQuery("Got Runtime...");
Process p = c.exec(String.format("cmd");
window.finishedQuery("Excecuted CMD");
2nd line doesn't work in the browser, but in the Netbeans applet viewer it does.
window is my applet, and it does show got runtime but not excecuted CMD.
In Google chrome I see the following message:
access denied ("java.io.FilePermission" "<<ALL FILES>>" "execute")
I guess the applet "thinks" that I want to get access to the users PC/programs, but I want to start a program on the server.
What do I have to do?

If created by the applet, the Process will be created in the JVM of the client machine. That VM will not be able to call methods on the server.
The best way to approach this is to have the sand-boxed applet call home to a web-service (servlet, JSP, PHP, ASP..) on the same host it comes from. Have the web-service create the Process1, and provide the output to the applet for consumption/display.
1.
Also, go through the Java World article linked from the runtime.exec Wiki & implement all the recommendations, but for 1.5+ use a ProcessBuilder to create the Process.

Okay, i wrote a service to create the process and created a "client-class" via wsimport...everything works fine in netbeans applet manager, but when i start the applet in my browser, the applet doesnt request the service, even when i put my applett on a local webserver(xampp) and start the service on the same client. i thought if both are on the same client, i dont have to sign my applet?
(note sure if i have to make this a new question or an answer to my question...)

Related

.bat file not running when executed by app as windows service

So, I have this client-server app which is written in 4th Dimension Language, that runs as a service so it restarts automatically if something happens to the server. This language has a built in function which allows you to run cmd commands and I have another java app in the same folder, that is in charge of sending emails, something my server side app cant handle. We use a command for running this jar from cmd and send the parameters from there, creating and xml for it to create an html from that and send it via email. The thing is when I run this command with the app running as a service, the command simply does not run, but, if I run the app normally, it works like charm, with no problems whatsoever.
At first I thought it could be the paths, so I got all the paths to be absolute, using the full route, yet it doesnt work still. Also I tried exporting the command as a bat and running it by hand, in the exact same path were the server is, and it works just fine. I thought that maybe the service needs some sort of admin privileges, so I started it as Admin from the service, but it changed nothing.
Is there any chance the service has some sort of limitation which doesnt allow the app to execute external commands? If so, is there anyway to bypass this limitation?
Well, I couldn't make it work when it was running by service, so I made a bat with the command the service uses and pasted it in the Windows Startup Folder, so it "opens manually" when windows starts. It's not the real answer, but is workaround for needing to use the service.

java web start app on localhost using WAMP is not able to run php scripts?

I have a signed java web start app that has been running on a server for many years. I have a need to create a version that I can run on a local machine. I have setup WAMP and the database and confirmed that these work by accessing through php scripts via localhost. My java program is also launching but there is a problem when it tries to access a local php file through locahost. Nothing is returned. I call php scripts that access the database from within the java app. Am i missing something fundamental here? ANy help appreciated. Thanks
Thanks for the responses. The problem has been solved. I forgot to turn off deprecation and other warnings so what I was getting back from the php scripts was not what I expected. Once I turned off the warnings everything functioned as expected. To sum up, I am serving up a java web start app through localhost, executing php scripts from within the java app that manipulate an sql database, and return the results to the java app.
Thanks again,
greg
Well are you using Quercus ? It would have been easier to suggest a solution if you had mentioned how you are running php scripts in java app.
Basic thing here is you should always call your php file using classpath or path associated within the project
PHP php = new PHP("classpath:/com/demo/phpfile.php");
or sometimes you could use(os path if set up correctly) path of file as accessed by you operating system.
C:\websites\www\myproject\phpfile.php(example windows path)
if you are accessing files like http://localhost/myproject/phpfile.php
then the php file is executed by the wamp server and html output returned. so you do not get the php script.
If you are trying to run java inside php then you could call php files in the manner http://localhost/myproject/phpfile.php to see the end result.

Understanding applets

I have to call .exe file on client.
But I dont understand sevler-client communication using Applets.
So few Q:
1.Can I do my task using Applets?
2.Does applets jar methods called on server?
Thanks in advance.
Applet does not maintain a state-full communication between client and server.
It is a Java application that runes inside the browser and has an access to a local system resources (if signed) and existing browser session, i.e. can use the same cookies to perform HTTP calls within the same Server-Side session.
1) Yes, you can do you task in Applet as in any other java application, however, in case of Applet it must be signed with the digital signature: http://www.oracle.com/technetwork/java/javase/tech/java-code-signing-1915323.html#60
2) No. All the classpath dependencies Applet may have, will be downloaded and cached on the client machine. See http://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/index.html
File system access is often not allowed as applets can (should) run in a sandbox limited-privileges environment. So running an exe file is possible only if the applet has proper permissions. Typically they are given such permissions when signed.
That being said, try not to use applets and write proper Java applications instead. You can always wrap the Java app in an applet, so that it is started from the applet.
Not so sure what you mean by the second question.

Hardware support from a web application

I have a web application running with support for some specific pieces of hardware. This is achieved in the following steps:
User runs a small installer that places java files (and a couple
others) on the client machine. The main piece is a jar called "hardwareManager"
User visits web app. The web app runs a java applet which, due to
a .java.policy file placed during the install, has permission to
interact with the client machine outside the browser sandbox.
The applet checks to make sure the hardwareManager is running,
and if not runs a command to start it.
User interacts with the web app which sends commands to the applet via
javascript. The applet then writes commands to a text file
on the client machine. The text file is constantly monitored by the
hardwareManager which runs any commands it reads in.
This works, but seems clunky. I have a couple ideas on how to improve it, but I don't know which, if any, are even worth trying.
Would it be better to set up the hardwareManager as a socketServer and have the applet connect directly to it, rather than going through text files? Is that even possible?
Is there a way to eliminate the applet altogether and have the javascript talk directly to the hardwareManager? Maybe by writing the hardwareManager to be a local http server? What port should it run on? Do javascript xss limitations fit in here somewhere?
It would be less clunky to start the Java application using Java Web Start. This would remove the need to daemonize or install the Java hardware manager.
Another alternative is to use a built-in browser inside Java. I supose this is not an option, since you depend heavily on Javascript (I suppose to provide a rich client experience).
If you already have to install something on the client machine, why did you make the choice to go with a web application?
Talking from experience: We had a Java EE application which needed to print to PoS printers at the client site. We installed a small "synchronizer" application that connects through SSH and synchronizes all clients files. Afterwards, it loads the JAR and executes the program. This program connects through RMI with the server and subscribes to a JMS queue to receive the print assignments.
Applied to your case: Why not let your Java application connect to the server directly? You can use HTTP, SOAP or even JMS over RMI. You can then launch the hardware command from the server (instead of from the limited JavaScript webbrowser environment). This way, you get tons of features: authentication, buffering of commands, and you can even share hardware between multiple clients.
Schematic:
<----AJAX------> Web browser
ApplicationServer
<---HTTP/SOAP--> Java hardware manager application
You can launch the Java application using Java Web Start, which allows you to update the application automatically (instead of needing to pass every client a new installer).

WebBrowser1 problem with external web app

the external Application works good in IE so i know the problem is inside My App.
The external web app is java applet and java script based, and the problem im experiensing is when the java applet tryes to run a java script. The Applet opens a seconday window with a java script and tryes to do some operations but then i just stops. And my Application seems to be non responsive and i have to restart it.
in IE it works just fine, the java applet opens the window does its thing and updates back in to the Applet.
anyone know why it not working in the webBrowser control?
Two scenerio could be possible:
1) whenever we opens the web site in web browser control any previous session(authentication) cookie is not being reused, and new session will be created.
2) you have to override the eventhandler webBrowser_newWindow to allow opening the new window.

Categories