Written by: Gui jingqiu
Reference: http://java.sun.com/j2se/1.5.0/docs/guide/javaws/developersguide/contents.html
Preface
The project is required, so I studied it a few days ago and spent 1.5 days without wasting any time. I would like to share it with you.
Application
JWS is mainly used to publish applications. It mainly writes JNLP files. When users access JWS through HTTP, the applications are automatically installed on the local machine. In the future, JWS will be automatically updated. It can be said that it is a new way to release software. (Of course, the other party must have a JVM)
Procedure
Compile a test file and compress it into a hello. jar package.
-
- // File hellojnlp. Java
- ImportJavax. Swing .*;
- ImportJava. AWT .*;
- ImportJava. AWT. event .*;
- ImportJava. Io .*;
- ImportJava.net .*;
-
- Public ClassHellojnlpExtends Jframe Implements Actionlistener{
- /**
- *
- */
- Private Static Final LongSerialversionuid = 1l;
-
- PublicHellojnlp (){
- Super("Hello JNLP ");
- StringLoadedfrom =This. Getclass (). getclassloader (). tostring ();
- JlabelJl =New Jlabel("Loaded by" + loadedfrom );
- JeditorpaneJtp =New Jeditorpane("Text/plain", "version 1.1.2 ");
- JbuttonBB =New Jbutton("Write native file ");
- Getcontentpane (). Add (JL,Borderlayout. North );
- Getcontentpane (). Add (jtp,Borderlayout. Center );
- Getcontentpane (). Add (BB,Borderlayout. South );
- BB. addactionlistener (This);
- BB. setactioncommand ("wnf ");
- }
-
- Public Static VoidMain (String[] ARGs ){
- JframeF =NewHellojnlp ();
- F. setbounds (100,100,325,250 );
- F. setdefaclocloseoperation (dispose_on_close );
- F. setvisible (True);
- F. addwindowlistener (New Windowadapter(){
- Public VoidWindowclosed (WindoweventE ){
- System. Out. println ("shutting down ...");
- System. Exit (0 );
- }
- });
- }
-
- Public VoidActionreceivmed (ActioneventArg0 ){
- // System. Out. println (arg0.getactioncommand ());
- Try{
- FileoutputstreamFos =New Fileoutputstream("Abcfile ");
- Try{
- FOS. Write (0 xFFFF );
- FOS. Close ();
- System. Out. println ("Write File OK ");
- }Catch(IoexceptionE ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }
- }Catch(FilenotfoundexceptionE ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }
- Try{
- Socket sock =NewSocket (Inetaddress. Getbyname ("www.cctv.com"), 80 );
- Sock. Close ();
- System. Out. println ("socket create OK ");
- }Catch(UnknownhostexceptionE ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }Catch(IoexceptionE ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }
- }
- }
-
This application tests the local file system and uses local network resources when clicking jbutton. (Compilation and packaging process omitted)
Next, I had a hard time working for a day. How can I perform certificate authentication on the jar so that the application can be installed to the user to operate on local resources. Originally, I used JDK's keytool to generate a certificate, but it was only available for half a year. Later I decided to apply for a certificate from the official website, which would cost 800 $/year. Finally, we should use OpenSSL for our own purposes, but the process is quite complicated and cannot control the process well. The main reason is that we are not familiar with OpenSSL commands. After a day of hard work, I finally found that Java itself could generate a user-defined deadline for use. Ah...
Generate a new keystore
Keytool-genkey-keystore mykeystore-alias myself
Generate a private certificate
Keytool-selfcert-alias myself-keystore mykeystore-validity 365
Note-validity 365 indicates the validity period of the certificate, which is 1 year. You can define a larger value by yourself.
Sign jar
Jarsigner-keystore mykeystore hello. jar myself
In this way, the signature is complete. Finally, write a JNLP description file hello. JNLP.
-
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! -- File hello. JNLP -->
- <JNLP codebase = "http: // 192.168.1.6: 81/JNLP /"
- Href = "http: // 192.168.1.6: 81/JNLP/Hello. JNLP">
- <Information>
- <Title> Hello </title>
- <Vendor> tech tips sample May 2001 </vendor>
- <Icon href = "/JNLP/qq.jpg"/>
- </Information>
- <Resources>
- <J2se version = "1.3 +"/>
- <Jar href = "/JNLP/Hello. Jar"/>
- </Resources>
- <Security>
- <All-permissions>
- </Security>
- <Application-Desc main-Class= "Hellojnlp"/>
- </JNLP>
-
I put jar and JNLP on the host of 192.168.1.6 and access them through 81port. You can modify the above information as needed.
Test it. Http: // 192.168.1.6: 81/JNLP/hello. if JNLP is opened with IE, a basket that you accept will pop up immediately, indicating that this individual or company is untrusted (because it is not a certificate applied by an authority, AH ), click "accept" to run the program in jar normally and use the local function normally.
Similarly, the applet can operate locally after the above steps, just like the signature ActiveX, or like some of IE's self-Signed ActiveX (which is actually a system vulnerability, huh, huh ), please criticize and correct.