anjinkristou

Java com access

Jun 10th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.96 KB | None | 0 0
  1. Well holy everlasting crap. Upon originally deciding to turn off the "next generation plug-in" option in the Java console with the intent of making my applet work in Firefox, I've inadvertently disabled JNLP. I only found this out by a last resort effort to "try something...ANYTHING". After turning it back on, the jnlp runs perfectly in IE. Firefox is of course the browser I need to use for my POS (because of a jsprint addon)...but of course it's not working. It won't download the JNLP file or if it is, something's going wrong. It just hangs. Nothing shows up, but my server logs say all the images and html is being downloaded. Not the jnlp or any of the jar files though. So it's either turn off the next generation plug-in and have security issues or turn it on and not be able to use Firefox. Is there ANYTHING I can do without having to resort to changing the policy file?
  2.  
  3. FWIW, I'll mark this as the solution and start a new thread somewhere else about my latest problem. Here's the most updated contents for everything needed to get the damned thing working (except in Firefox):
  4.  
  5. First off, make sure the PATH and CLASSPATH environment variables are set in Windows. PATH should be C:\Program Files (x86)\Java\jdk1.7.0_01\bin (change to your own jdk directory if you have a different version). CLASSPATH should be wherever you make your java classes. Mine is D:\Files\Java\Classes. If you don't set the PATH variable, you can't run 'java', 'javac' or 'jarsigner' from any directory. You'd have to be in the bin directory of jdk. Make these variables for your User account...not System (as there could already be env variables named like these).
  6.  
  7. Next, for signing your applets, create your keystore:
  8.  
  9. keytool -genkey -dname "cn=My Company, ou=Whatever, o=My Company, c=CA" -alias mycompany -keystore "D:\Files\Java\Certificates\certfile" -storepass MY_PASSWORD -validity 180
  10. Check online tutorials to make sure you know what each of these arguments are for.
  11.  
  12. .BAT file I made to easily create necessary .jar file from .java file and sign it. Just make a shortcut or run the .bat file instead of having to do this by hand every time you make a change to the .java file
  13.  
  14. @echo off
  15.  
  16. set certificate_password=MY_PASSWORD
  17. set certificate_alias=myalias
  18. set package=mycompany
  19. set class_file=PortWriter
  20. rem class_path is where my .java file resides (...\java\Classes\mycompany\PortWriter.java)
  21. set class_path=D:\Files\Java\Classes
  22. set certificate_loc=D:\Files\Java\Certificates\certfile
  23. rem final_loc is the assets folder where the .jar file will reside
  24. set final_loc=D:\Files\Websites\pos\app\assets\java
  25.  
  26. cd "%class_path%"
  27. rem Change to "D:" otherwise Windows won't *actually* change directories from C: to D:
  28. D:
  29.  
  30. javac -Xlint:unchecked "%package%\%class_file%.java"
  31. pause
  32. jar cfm "%final_loc%\%class_file%.jar" "%package%\Mainfest.txt" "%package%\*.class"
  33. pause
  34. del "%package%\*.class"
  35. jarsigner -keystore "%certificate_loc%" -storepass %certificate_password% "%final_loc%\%class_file%.jar" %certificate_alias%
  36. pause
  37. Mainfest.txt (make sure there is a carriage return after the "Main-Class" line. You shouldn't need this Manifest.txt file if you want to specify the main-class in your JNLP.): Main-Class: mycompany.PortWriter
  38.  
  39. Java File:
  40.  
  41. package mycompany;
  42.  
  43. import com.engidea.comm.CommPort;
  44. import com.engidea.comm.CommPortIdentifier;
  45. import com.engidea.comm.SerialPort;
  46. import com.engidea.comm.SerialPortEvent;
  47. import com.engidea.comm.SerialPortEventListener;
  48. import com.engidea.win32jcom.WinjcomIdentifier;
  49. import java.io.*;
  50. import java.util.Iterator;
  51. import java.util.List;
  52. import java.applet.*;
  53. import java.security.*;
  54.  
  55. /*
  56. WinJcom is a native interface to serial ports in java.
  57. Copyright 2007 by Damiano Bolla, [email protected]
  58.  
  59. This library is free software; you can redistribute it and/or
  60. modify it under the terms of the GNU Library General Public
  61. License as published by the Free Software Foundation; either
  62. version 2 of the License, or (at your option) any later version.
  63. This can be used with commercial products and you are not obliged
  64. to share your work with anybody.
  65.  
  66. This library is distributed in the hope that it will be useful,
  67. but WITHOUT ANY WARRANTY; without even the implied warranty of
  68. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  69. Library General Public License for more details.
  70.  
  71. You should have received a copy of the GNU Library General Public
  72. License along with this library; if not, write to the Free
  73. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  74. */
  75.  
  76. /**
  77. * Simple class that can list system ports and allow IO
  78. */
  79. public class PortWriter extends Applet {
  80.  
  81. private CommPortIdentifier portIdentifier;
  82. private SerialPort serport;
  83.  
  84. public static void main(String[] args) {}
  85. public void init() {
  86. System.out.println("Booting RS232 Java Applet...");
  87. }
  88.  
  89. public void Sends(String port, String message) {
  90.  
  91. final String com_port = port;
  92. final String send_message = message;
  93.  
  94. AccessController.doPrivileged(new PrivilegedAction<Object>() {
  95. public Object run() {
  96.  
  97. try {
  98. portIdentifier = new WinjcomIdentifier(0);
  99. try {
  100. selectComport(com_port);
  101. new Thread(new PortReader()).start();
  102. try {
  103. System.out.println(com_port + ": " + send_message);
  104. typeSendBytes(send_message);
  105. } catch (Exception e) {
  106. System.out.println("Couldn't send data to " + com_port);
  107. }
  108. } catch (IOException e) {
  109. System.out.println("Couldn't connect to " + com_port);
  110. }
  111. } catch (Exception e) {
  112. System.out.println(e);
  113. }
  114. return null;
  115. }
  116. });
  117. }
  118.  
  119. private void typeSendBytes( String message ) {
  120. try {
  121. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  122. String aStr = "";
  123. if (aStr != null) {
  124. aStr = message + "\r\n";
  125. // WARNING: be careful, you shoulod select the encoding !
  126. // This will timeout if you have FLOW CONTROL and theline is stuck !
  127. byte []buf = aStr.getBytes();
  128. serport.write(buf,0,buf.length);
  129. }
  130. } catch ( Exception exc ) {
  131. exc.printStackTrace();
  132. }
  133. }
  134.  
  135. private SerialPort openPort ( String portName ) throws IOException {
  136. try {
  137. CommPort aPort = portIdentifier.getCommPort(portName);
  138. aPort.open();
  139. return (SerialPort)aPort;
  140. }
  141. catch ( Exception exc ) {
  142. //System.out.println("exc="+exc);
  143. //exc.printStackTrace();
  144. throw exc;
  145. }
  146. }
  147.  
  148. private void selectComport ( String portName ) throws IOException {
  149.  
  150. try {
  151. serport = openPort(portName);
  152. serport.setSerialPortParams(9600,8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
  153. serport.enableReceiveTimeout(20000);
  154. serport.setEventListener(new EventListener());
  155.  
  156. serport.notifyOnDSR(true);
  157. serport.notifyOnCarrierDetect(true);
  158. serport.notifyOnCTS(true);
  159. } catch (IOException exc) {
  160. //System.out.println("Exc="+exc);
  161. //exc.printStackTrace();
  162. throw exc;
  163. }
  164.  
  165. }
  166.  
  167.  
  168. private final class EventListener implements SerialPortEventListener
  169. {
  170. public void serialEvent(SerialPortEvent ev)
  171. {
  172. System.out.println("Got event="+ev);
  173. }
  174. }
  175.  
  176.  
  177. private final class PortReader implements Runnable
  178. {
  179. public void run()
  180. {
  181. try
  182. {
  183. // This will timeout if nothing is received in the specified time.
  184. byte []buff = new byte[1];
  185. while ( serport.read(buff,0,buff.length) > 0 )
  186. {
  187. // NOTE: you should be checking the encoding !
  188. System.out.print(new String(buff));
  189. }
  190. }
  191. catch ( Exception exc )
  192. {
  193. exc.printStackTrace();
  194. }
  195. }
  196. }
  197.  
  198.  
  199. }
  200. JNLP File:
  201.  
  202. <?xml version="1.0" encoding="utf-8"?>
  203. <jnlp>
  204. <information>
  205. <title>RS232 Communication Applet</title>
  206. <vendor>My Company</vendor>
  207. <description>RS232 Applet for communicating with POS Display Pole</description>
  208. <offline-allowed />
  209. </information>
  210. <security>
  211. <all-permissions/>
  212. </security>
  213. <resources>
  214. <j2se version="1.7.0+" />
  215. <jar href="PortWriter.jar" part="com" main="true" />
  216. <jar href="jcomport.jar" part="com" />
  217. <nativelib href="winjcom.jar" part="com" />
  218. </resources>
  219. <applet-desc
  220. name="RS232 Communication Applet"
  221. main-class="mycompany.PortWriter"
  222. width="1" height="1" />
  223. </jnlp>
  224. HTML:
  225.  
  226. <applet id="SerialPort" width="1" height="1" codebase="/assets/">
  227. <param name="jnlp_href" value="PortWriter.jnlp">
  228. </applet>
  229. I had taken the winjcom.dll file and "jarred" it and signed it with the same certfile. I made sure to cd to the same directory in which winjcom was so that it would be in the root of the .jar file. I also took the jcomport.jar file provided by the author of winjcom and re-signed it with the same certfile. That being said, all .jar files have been signed by the same certfile.
  230.  
  231. I hope this helps out anyone who's having as much trouble as I did.
Advertisement
Add Comment
Please, Sign In to add comment