Java FAQ Highlights (from Sun China Official Station)

zhaozj2021-02-16  61

How to set up Java 2 (JDK1.2) environment variables? A: After Java 2 installation, you need to set the path and java_home environment variables. Different from JDK1.1: After setting the Java_Home environment variable, JVM will automatically search system class libraries And the current path of the user. The setting of the Java 2 environment variable is shown in the following example: Solaris platform: STENV Java_Home Java2 installation path setenv path $ java_home / bin: $ {pathing} Windows platform: 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 An error occurred when running a Java applet in the Netscape or IE browser. How do I determine an error range? A: When Java Applet is running in the browser, use the browser itself's default JVM. Different browser supports JDK support The degree is also the same. Therefore, there is an error in the 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 incorrect generation is with the browser. Related. If the applet runs everything in AppletViewer or Hotjava, the error is caused by the browser is not fully compatible with JDK. At this point, the solution can be Java Plugin using the Hotjava browser or installing Sun. If applet 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, sometimes the Chinese characters will be displayed as chaos? BR / >? A: This problem is usually related to the implementation of each JDBC Driver. At present, most JDBC Driver uses local encoding formats to transmit Chinese characters, such as Chinese characters "0x4175" will be transferred into "0x41" and "0x75" to transmit So we need to convert characters returned by JDBC Driver and sending characters to JDBC Driver. When using JDBC Driver to insert data into the database, you need to turn unicode to Native. CODE; When 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.Length (); i ) {ix (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. configuration typically requires 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: There are two kinds The method can set the ClassPath environment variable of Java WebServer 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 is started, the classes directory is automatically added to the ClassPath environment There is a variable. Modify the httpd.nojre file, add the path name of the user class file to the ClassPath environment variable. Q: Why use naming.lookup on the Windows platform to get the remote RMI object when you get the remote RMI object? A: Machine Network settings are incorrect and probable can cause this problem The occurrence. RMI uses the Java network class, especially the java.net.inetaddress class, which will query the host name of TCP / IP, including the IP address to the host name mapping and the host name to the IP address. On the Windows platform, This query function is implemented by the local Windows Socket library. So the delay is in the Windows library, not in the RMI. If your machine is set to use DNS, the problem is usually the DNS server can't find the host name. 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 file WinntSystem32DriverseTCHOSTS or WindowsHosts. The format is as follows: IP address hostname so setting should be obvious Decrease the time spending the query. Q: How to set up the Java Application to access the external website? A: If you access the external website in the Java Application, you should first set the proxy information, the 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); connect =

(HttpurlConnection); ..... Q: Swing component jlist's list data modified, how to notify JList change display? A: Jlist component has a separate display mode ListModel to represent Jlist's display data. Jlist After creating, the number of jlist data elements and the number of data elements can dynamically change. Jlist Observe the changes in data in its data mode ListModel. Therefore, a ListModel's correct implementation should notify events when each data is changed. Listener. When using a constructor jlist (Object []), a DEFAULTLISTMODEL will automatically create an instance of a DEFAULTLISTMODEL to store the Jlist's display data, and you can call the simple way to DEFAULTLISTMODEL to dynamically modify the JList data. , Such as removeelelementat (index), addelement (Object), etc. DEFAULTLISTMODEL will notify Jlist to change the data on data while modifying data. Q: How do I implement a mode dialog in the Java applet? A: Implement mode dialogue in Java Applet The key to the box is to specify a correct parent window for the dialog when creating a dialog. Because applet is a subclass of the Panel class, you cannot use the parent window of the dialog, so we must first get the window where the applet is located. As the parent window of the pattern dialog. Sample code is as follows: ..... Dialog D = New Dialog (getParentWindow (Comp), Title); // CoMP is any component on the applet .... public void getParentWindow (PUBLIC VOID GETPARETWINDOW) Component componapplet, string title) {Container C = componapplet.getParent (); while (c! = Null) {if (c @) c; c = c.getParent ();} return null;} : How to display another HTML page in the Java Applet? A: AppletContext related to the applet through the java.applet.applet.getAppletContext () method, appletContext.show The Document (URL) method can make the browser in the app to display another web page. Q: Can you run the signature applet implemented by JDK, can you run in Netscape or IE? A: Signature applet implemented with JDK, can not be in Netscape or IE Operation, but you can run in the Hotjava browser .. Different browsers provide different signature Applet mechanisms, such as Netscape provides the ZigBert tool and Capability API, and IE requires using a CAB file. However, whether it is Netscape tools The signature applet, or the signature applet generated by IE, can not be run in other browsers. If you want to make JDK generated signage Applets can run in Netscape or IE, the solution is to install Java Plugin in Netscape or IE The signature applet implemented with JDK can run in both browsers. Q: Use JNI technology to call the C library from the Java application, but how to make the C library can call another C program? BR / >? A: If a C library C1 called by Java still needs to call another C library C2, then when compiling C1? BR /> φ? Br />

The join library C2, the steps are as follows (Solaris platform): Write the Java file calling the C library, compile. Javac Java file name to generate a C program header file Javah -jni Java file name (without suffix .java) Written by Java call C procedure C1.c, and C2.c called C1, and compile .cc -g -iinclude path name c2.c -o libc2.socc -g -iinclude path name -LC2 c1.c -o libc1.so set Environmental Variable STENV LD_LIBRARY_PATH LIBC1.SO, libc2.so location path: $ {ld_library_path} Run Java App Quality: How to list all drive names in the PC file system in Java language? A: In Java 2, Java The File class in the .IO package adds the method Listroots () to achieve this. Q: Why is Runtime.exec ("ls" without any output? A: Call the runtime.exec method will generate a local process and return An example of a Process subclass, this example? Br /> 捎? Br /> related information about the process or acquisition process. Since the child process created by the runtime.exec method does not have its own terminal or console, the child The standard IO of the process IO (such as stdin, stdou, stderr) is redirected by process.getoutputStream (), process.getinputstream (), process.getRRORSTREAM () method. User needs to use these stream to sub-process Enter data or get the output of the child process. So correct the runtime.exec ("LS" routine is as follows: try {process = runtime.getime (); invutstreamReader Ir = newinputstreamReader (process.getinputStream ()) LINENUMBERREADER INPUT = New LinenumberReader (IR); String line; while ((line = INPUT.READLINE ())! = Null) System.out.println (line);} catch (java.io iexception e) {system. Err.Println ("IOE Xception " 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 Applets, you need to pass the following steps 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 install 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 use KeyTool -Cer Treq applies for an e-certificate to the commercial CA center. Q: If you write Object to a file through ObjectOutputStream multiple times in an additional manner, use ObjectInputStream to read these objects to generate streamcorruptedException? A: When using the default Serialize, The constructor of an ObjectOutputStream and an ObjectInputStream constructor must correspond to the constructor of ObjectOutputStream written to the output stream, while ObjectInputStream first reads this identity head. So, multiple times in a file Object is written, the file header will contain a plurality of identification. when it is used to deserialize the ObjectOutputStream ObjectInputStream, StreamCorruptedException generated. one solution is a class constructor ObjectOutputStream and covering the writeStreamHeader () method. covered The latter WriteStreamHeader () method should determine if the Object is written to the file for the first time, and the carbonarbon is called, if, if you write Object, you should call the ObjectOutputStream.Reset () method. Q: The Serialization class is a flow-oriented. How should I write to a random access file? A: Currently, there is no direct method to write objects into a random access file. But can Use the ByTearRay input / output stream as an intermediation to write or from random access to random access files? Br /> BR />

Read the byte, and you can use byte flow to create object input / output flow for reading and writing objects. Need not to note that in the byte stream, you want to include a complete object, otherwise read and write the object. Error. For example, Java.io.byteArrayoutputStream can be used to get the byte stream of ObjectOutputStream, which can be obtained from the Byte array and can be written to the random access file. Instead, we can read bytes from a random access file. Array, use it to construct ByTearRayInputStream, which can be constructed to read objects. Q: When running RMI applications, you can not start the name service RMIREGISTRY, but start from the program? A: Yes. Java.rmi package The class java.rmi.registry.locateREGISTRY.LOCATEREGISTRY is provided to get the 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 get the name service. Q: How should I set the printer name and other print attributes when using class printjob? A: Use the following method to get PrintJob instance for control Print Action: Toolkit.getPrintJob (Frame F, String Jobtitle, Properties Pro) So settings for printing properties can be implemented by setting the Prop properties, print 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.ORIATION: Print direction, can be" Portrait "or" Landscape "AWT.PRINT.PAPERSIZE: Paper size, can be" Letter "," Legal "," Executive "or" A4 "Q: Suspend () and resume are defined in JDK1.1. Method, but in JDK1.2, it should be alternative to? BR /> lemon? Br /> method? A: Thread.suspend itself is easy to generate a deadlock. If a target thread is connected to a key system resource? BR /> Child? Br />

转载请注明原文地址:https://www.9cbs.com/read-19002.html

New Post(0)