Running jar in client from browser

Asked

Viewed 4,083 times

6

I need to run a jar file that should be on the client’s pc over the web and pass some parameters to this jar, I managed to do this with the exec() php:

exec('java -jar "C:\Users\Suporte01\Documents\NetBeansProjects\Printer\dist\Printer.jar" Hello', $output);
print_r($output);

but it executes the jar on the server...

EDIT

It would be something like I see some web applications do like the logmein, that opens directly from the browser a remote access program that gets installed on your computer, as can be seen below:

inserir a descrição da imagem aqui

Is it necessary to have some service installed on the customer?

  • Leaving a link here that may help in the future :)
 http://stackoverflow.com/questions/11922225/how-to-use-php-exec-to-run-jar-with-input-parameters

  • Dude, do the following, play the jar on the client and create a link in the browser to open a page accessing a server installed on the client and makes this server"client" invoke the jar... to searching how to do too and I just got this idea... I haven’t even tried it yet! hopefully it works!

3 answers

6

The exec() PHP runs a command server-side, since it is not a language client-side.

Running a JAR through the browser

To execute a in the browser, as you wish, what I would indicate is to use a applet, as seen in this reply and call it by javascript, as in the following example:

<applet name="myapp" archive="myjar.jar" code="com.company.MyApplet"/>
<script>
   var result = myapp.foo();
</script>

Running a JAR that is on the client’s computer by Internet Explorer

Another possibility, after seeing what you wanted from the issue issue, is to request, through , that the customer turns the for ActiveXObject, as can be seen here. The example to run is this:

function RunExe(){  
  var w = new ActiveXObject("WScript.Shell");
  var myJar = 'C://WindowJar.jar'; //exemplo
  w.run(myJar);//Roda o jar
  return true;
}

If you want to work some return of the executed file, you can use the following example:

function RunExe(){  
  var w = new ActiveXObject("WScript.Shell");
  var myJar = 'C://WindowJar.jar'; //exemplo
  var ex = w.run(myJar);//Roda o jar
  var ret = "";
  //lê a saída do jar
  while (!ex.StdOut.AtEndOfStream) {
    ret += ex.StdOut.ReadLine();
  }
  //Dá um alert no retorno
  alert(ret)
  return true;
}

Running a JAR that is on the client’s computer using protocols

Another alternative, not indicated for safety reasons, is to use URL protocols, which means tampering with the records of the target machine. According to this example, it is possible to achieve this objective as follows:

Create a personified protocol

[HKEY_CLASSES_ROOT\protocolname]
@="URL: descricao"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\protocolname\shell]

[HKEY_CLASSES_ROOT\protocolname\shell\open]

[HKEY_CLASSES_ROOT\protocolname\shell\open\command]
@="\"C:\\Path to\\Jar\\myJar.jar\" %1"

Using a reference tag in HTML

<a href="protocolname:parameter-value">link</a>

However, the following may be removed from the abovementioned documentation::

Security Alert Applications that Handle URL protocols must consider how to Respond to Malicious data. Because Handler Applications can receive data from untrusted sources, the URL and other Parameter values passed to the application may contain Malicious data that Attempts to exploit the Handling application.

In free translation:

Security Alert Applications that manipulate URL protocols should consider how to respond to malicious data. Because the application handler may receive data from unreliable sources, the URL or other parameter values passed to the application may contain malicious data that tries to exploit the manipulated application.

Therefore, if it is not strictly necessary to use another browser, I recommend using Internet Explorer with the ActiveXObject, for being simpler and less dangerous than creating a custom protocol.

  • So @Felipe Avelar tried to do the same using an applet but when I run the applet direct by netbeans it usually works by pulling the printers installed in the client which is what I need, plus when running in the browser it does not pull them..

  • 1

    Dude, what do you need to do is just print it out or just get all the printers? If I am not mistaken, an applet will not be allowed to order a client’s printers...

  • So I need to print a RAW DOCUMENT... I need to be able to at least open the windows print dialog, but the right way would be to run the application on the client, like this image example

  • If it is an HTML page that you will print, why not use the method of printing the browser itself, using the window.print()?

  • Exact is not a html page I will print, I need to print in EPL format on a zebra printer, I cannot send an html document for printing...

  • Example: http://stackoverflow.com/questions/9308412/sending-a-barcode-to-a-zebra-printer-from-a-java-application

  • I’m just speculating, that’s why the comment, but what if you stored the JAR on local Storage browser and use the local path of that file, now in the client’s PC, as argument for that Felipe Activex?

  • when executing the code it returns an error Uncaught ReferenceError: ActiveXObject is not defined

  • This option only works for Internet Exeplorer, I will edit my answer with another alternative.

Show 4 more comments

1

To run a JAR on the client you have some options:

This site contains comparisons between the two methods: http://mindprod.com/jgloss/javawebstart.html#APPLETSVSJWS.

It’s all in English, so I’ll try to enumerate the main differences and similarities here:

  • Both run in a "restricted" environment if the JAR is not digitally signed;
  • In order to access restricted areas, you need to describe what you will do, and sign the JAR;
  • An applet runs in the browser, Java web start looks like a remote application, but it is downloaded from a server automatically.

The last option would be to "give" the JAR to the client and ask it to run directly on the client, and you could access your server to upload the information you need - in this case, your JAR would have unrestricted access to what the user can see (at least without the restrictions of a Java Web Start applet or application).

1

The jar at all times will run on the server side. What you have to do is capture the execution output and send it to the client side.

I think the wisest, in this case, would be to put your jar inside a web service, and this service offer a REST API for other services to talk to it via HTTP.

  • There would be no way because I’m using java to access the client’s printers...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.