Published: 91 readers have read this article on November 28, 2003
Java FAQs: How to set up the environment variables of Java 2 (JDK1.2)? A: Java 2 After installation, you need to set the PATH and JAVA_HOME environment variables. Different from JDK1.1: Set the Java_Home environment variable, JVM The system class library and the user's current path. The setting of the Java 2 environment variable is shown in the following example: Solaris Platform: SetENV Java_Home Java2 installation path setenv path $ java_home / bin: set java_home = java2 Installation path set path = $ java_homebin;% PATH% Q: What Java integrated development tools support Java 2? A: The current popular Java integrated development environment, such as Inprise's JBuilder, Symantec's Visual Cafe, Sybase's Powerj, support Java 2. Q: If there is an error when running the Java Applet in the Netscape or IE browser, how do I determine an error range? A: When the Java Applet is running in the browser, use the browser itself's default JVM. The browser has different levels of support for JDK. Therefore, running Java Applet in Netscape or IE browser, it is recommended to use the tool AppletViewer or Sun's HotJava browser provided by JDK to test the applet to determine the error. The generation is related to the browser. If the applet runs everything in AppletViewer or HotJava, the error is caused by the browser is not completely compatible with JDK. At this time, the solution can be using the Hotjava browser or install Sun Company Java Plugin. If the applet runs in the HotJava browser or AppletViewer, you should check the Applet program based on the error message. Q: When using JDBC to insert data into the database or extract data from the database, why is the Chinese characters Display as garbled? A: This issue is usually related to the implementation of each JDBC Driver. At present, most JDBC Driver uses local coding format to transmit Chinese characters, such as Chinese characters "0x4175" will be converted to "0x41" and "0x75" Transfer. Therefore, we need to convert characters returned by JDBC Driver and to send characters to JDBC DRIVER. When using JDBC Driver When you insert data in the database, you need to transfer Unicode to Native Code; when you query data from the database, you need to convert Native Code into unicode. The implementation of these two conversions is given: String Native2Unicode (String S ) {IF (s == null || s.LENGTH () == 0) {return null;} Byte [] buffer = new byte [S.Length ()]; for (int i = 0; i s.ley (); i ) {IF (S.Charat (i)> = 0x100) {c = s.Charat (i); byte [] buf = (" c) .GetBytes (); buffer [j ] = ( Char) BUF [0]; buffer [j ] = (char) BUF [1];} else {buffer [j ] = S.Charat (i);}} return new string (buffer, 0, j);
} In addition to using two methods, some JDBC Driver If the JDBC Driver Manager sets the correct character set properties, the above 2 methods are not needed. Q: When using servlets to process HTTP requests and generate returned HTML pages How to make the Chinese characters in the HTML page you can display properly? A: javax.servlet.http.httpResponse class is used to generate return pages. Methods defined by httpResponse GetputStream () can get instances of servletOutputStream, so users can take servletoutputstream The .write method writes the contents of the return page in the output stream. But servletOutputStream uses the default encoding method. If you want to make the Chinese characters in the return page, it is best to specify the character encoding used in the specified manner. Usually need to construct a OutputStreamWriter, routine follows: public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {res.setContentType ( "text / html"); ServletOutputStream out = res.getOutputStream (); OutputStreamWriter ow = new OutputStreamWriter ( OUT, "GB2312"); OW.WRITE ("This is the test"); OW.Flush () ;OW.Close ();} Q: How do I set the ClassPath of Java WebServer to include the user's class file? A: Yes Both methods can set the ClassPath environment variable of Java Web Server to use the servlet written by the user to call the user's class file. Place the user's Class file in the javawebser_dir / classes directory, when the Java Webserver starts, the class of the class is automatically added. ClassPath environment variables. Modify httpd.nojre files, add the path names of the user class file to the ClassPath environment variable. Q: Why use naming.lookup on the Windows platform to get remote RMI objects What will be slower? A: The machine's network settings are incorrectly probably caused by this problem. RMI uses Java network classes, especially Java.Net.Netaddress classes, which will query TCP / IP host name, including IP The address to the host name mapping and host named the IP address map. In the Windows platform, this query function is implemented by the local Windows Socket library. So the delay is in the Windows library, not the RMI. If Your machine is set to use DNS, the problem is usually the DNS server can't find the host name, and the delay you find is the delay of the DNS query. Please try to add all hostname / IP addresses involved in the RMI communication to the local Document WinntSystem32Driverseetchosts or WindowsHosts. The format is as follows: IP address hostname should significantly reduce the time spending the query. Q: How to set up the Java Application to access the external website? A: If you are in Java Application Access external website, first set up Proxy information, sample code is as follows: import java.util.properties; ..... Properties sys = system.getProperties (); sys.put ("proxyset", "true"); Sys.put ("proxyhost", "myhttp.proxyserver.com"); sys.put ("proxyport", "80"); System.SetProperties (sys); u =
NEW URL (Website); connection = (httpurconnection) u.openconnection (); ..... Q: Swing component jlist's list data modified, how to inform JList change display? A: Jlist component has a separate display mode ListModel To represent jlist's display data. After jlist creation, the value of the JList data element and the number of data elements can be dynamically changed. Jlist observes the change in data in its data mode ListModel. Therefore, a ListModel's correct implementation should be in each time When the data changes, the listener of the event is notified. When using the constructor jlist (Object []) created a JList's instance, the system will automatically create an instance of the DefaultListModel to store the display data of Jlist, which can call the defaultListModel to be used. Method To dynamically modify the data of JLIST, such as RemoveElementat (INDEX), ADDELEMENT (Object), etc. DEFAULTLISTMODEL will notify Jlist for data changes while modifying data. Q: How do I implement a mode dialog in the Java Applet? Answer : The key to implementing the mode dialog box in the Java applet is to specify a correct parent window for the dialog when you create a dialog. Because the applet is the subclass of the Panel class, it is not a parent window as a dialog, so First get the window where Applet is located, the parent window of the Mode dialog. Sample code is as follows: ..... Dialog D = New Dialog (getParentWindow (Comp), title); // CoMP is any component on the applet .... Public void getParentWindow (Component Componpplet, String Title) {Container C = ComponApplet.getParent (); While (C! = NULL) {if (c = c.getParent (frame) C; c = c.getParent } Return NULL;
} Q: How to display another HTML page in the Java applet? A: You can get the appletContext () method to make the applet to browse the applet () method through the java.applet.applet.getappletcontext () method. Displays another web page. Q: Can I run with JDK implementation, can I run in Netscape or IE? A: The signature applet implemented with JDK can not be run in Netscape or IE, but can run in the Hotjava browser. Different browsers provide different signature Applet mechanisms, such as Netscape provides Zigbert tools and Capability APIs, while IE requires using a CAB file. However, whether it is the signature applet generated by the Netscape tool, or the signature APPPlet generated by IE, You cannot run in other browsers. If you want to make the JDK generated signage Applet to run in Netscape or IE, the solution is to install Java Plugin in Netscape or IE, you can use the JDK-implemented signature applet can be in these two The browser is running. Q: Use JNI technology can call the C subscription from the Java application, but how to make the C library can call another C subscription? A: If a C library C1 called by Java is still needed Call another C library C2, then connect the library C2 when compiling C1, the steps are as follows (Solaris Platform): Write the Java file calling the C library, compile. Javac Java file name generating C program header file Javah -jni The Java file name (without suffix .java) writes the C program C1.c called by Java, and C2.c called C1, and compiled. cc -g -iinclude path name c2.c -o libc2.so cc - G-INCLUDE path name -lc2 c1.c -o libc1.so Set the environment variable Stenv ld_library_path libc1.so, libc2.so location path: $ {ld_library_path} Run Java application Q: How to list PC files in Java language All drive names in the system A: In Java 2, the File class in the Java.IO package adds the method LISTROTS () to achieve this. Q: Why is Runtime.exec ( "ls") does not have any output? A: Call the runtime.exec method will generate a local process and return an instance of a process subclass, which can be used to control the process or acquire the process. Due to the call runtime.exec method The sub-process created does not have its own terminal or console, so the standard IO of the child process is re-directed through process.getoutputstream (), process.getinputstream (), process.geterrorStream () method Give it the parent process. Users need to use these stream to enter data or get the output of the child process to the child process. So correct the runtime.exec ("ls") of Runtime.exec ("ls") is as follows: try {process = runtime.getime () .exec (command); InputStreamReader ir = newInputStreamReader (process.getInputStream ()); LineNumberReader input = new LineNumberReader (ir); String line; while (! (line = input.readLine ()) = null) System.out.println (line);} catch (java.io ioException e) {system.err.println ("IOException" E.GetMessage ());
} Q: How to generate a signature applet so that the applet can access local resources? A: In JDK1.1, you can use the javakey command to generate public key, private key, certificate and signature JAR file, please refer to: http: //java.sun.com/security/usingjavakey.html and Java 2 have a relatively large improvement on the signature mechanism, allowing users to set security permissions more flexibly. Java 2 provides three tools: KeyTool, PolicyTool and Jarsigner are implemented Signing applet. For example, Joe has written a signature applet: signedapplet.java, then generating a simple signature applet is as follows: // Generate a key, the key is not named Joe, the password is SIGN12, stored in the keystore Joestore KeyTool -Genkey -Alaias Joe -KeyPass Sign12 -keyStore Joestore // Package SignedApplet.class and related files into JAR file JAR CVF SIGNEDAPPLETDEMO.JAR // Generate self-signed certificate generated by KeyTool to generate a signature applet (JAR file) Jarsigner -KeyStore Joestore -SignedJar Joe.jar Signedappletdemo.jar Joe // Output Self-signed Certificate from KeyStore to file keytore-ketport-ready, joesteore -alias joe -file joe.cer, need to pass the acceptor Susan for signing Applet Safely perform Joe written signature applet: // get JoE certificate and read it into the keytore in the keystore in the keystore, the keytore-IMPORT -ALIAS JOE-FILE JOE.CER -KEYSTORE SUSANSTORE / / Run PolicyTool to meet the Susan requirements Policy file policy // runs with AppletViewer or runs Java Plugin in your browser. For the deployment of the signature applet in Java Plugin, please refer to the following page: http://java.sun.com/security/signexample12/ Note: The above example is simply, using the self-signed certificate generated by KeyTool. In fact, users can also apply for electronic certificates to the commercial CA center using KeyTool -Certreq. Q: If the ObjectOutputStream is written to Object multiple times in an additional manner, why use ObjectInputStream to read these objects? A: Use When the default Serialize is implemented, an ObjectOutputStream constructor and an ObjectInputStream constructor must correspond one by one. The constructor of ObjectOutputStream is written to the output stream, and ObjectInputStream will first read this identity head. So, more When writing Object in an additional manner, the file will contain multiple identification heads. So use ObjectInputStream to deserialIze this ObjectOutstream, a STREAMCORRUPTEDEXCEPTION will produce a solution to construct an ObjectOutputStream subclass, and Override WriteStreamHeader () method. The overridden WriteStreamHeader () method should determine if it is written to Object in the file for the first time, carbonyl? Call super.writestReamHeader ();
If you write Object when you add an additional manner, you should call the ObjectOutputStream.Reset () method. Q: The Serialization class is a flow-oriented, how should you write objects to a random access file? A: Currently, there is no direct method to write objects into random access files. But you can use the ByteArray input / output stream as an intermediary to read or read or read the word from the random access file. Section, and you can use byte stream to create object input / output streams for reading and writing. Need not to note that in the byte stream, you want to include a complete object, otherwise an error will occur when reading and writing objects. For example, Java.io.byteArrayOutputStream can be used to get the byte stream of ObjectOutputStream, which can be obtained from the BYTE array and write it into a random access file. Instead, we can read the byte array from the random access file, using it ByTearRayinputStream can be constructed to read ObjectInputStream to read objects. Q: When running RMI applications, you can not manually start the name service RMIRGISTRY, but start from the program? A: Yes. Java.rmi package provides classes Java.rmi.Registry.locateREGISTRY, is used to get a name service or create a name service. Call the locateregistry.createRegistry (int port) method can create a name service at a particular port, so that users do not need to manually start RMIREGISTRY. In addition, LocateRegistry.getRegistry (String Host, INT port) method can be used to obtain a name service. Q: How should I set the printing attribute of the printer name when using the class printjob? A: Use the following method to get PrintJob instance to control the print operation: Toolkit .GetPrintJob (Frame F, String Jobtitle, Properties Pro) So settings for printing properties can be implemented by setting Prop properties, printing properties include: aff.print.destination: can be "printer" or "file" AWT.Print .printer: Printer Name AWT.PRINT.FileName: Print file name AWT.PRINT.NUMCOPIES: Costs AWT.PRINT.Options: Print command print options AWT .print.orientation: Printing direction, can be "portrait" or "landscape" aff.print.papersize: Paper size, can be "letter", "legal", "executive" or "a4"