Q: How do I set up the environment variable 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: After setting the Java_Home environment variable, JVM will automatically search 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: STENV Java_home java2 installation path setenv path $ java_home / bin: $ {path} 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 the INPRISE JBuilder, Symantec's Visual Cafe, Sybase's Powerj support Java 2.
Q: If an error occurs when running a Java Applet in a Netscape or IE browser, how do I determine the wrong range?
A: When Java Applet is running in the browser, uses the default JVM of the browser itself. Different browsers have different support for JDK. Therefore, running Java Applet in Netscape or IE browser. Error, it is recommended to use the Tool AppletViewer or Sun's HotJava browser provided by JDK to test the applet to determine the error of the error is related to the browser. 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 install Sun. If Applet is in Hotjava In the browser or AppletViewer, the APPLET program should be checked by the error message.
Q: When using JDBC into the database or extracts data from the database, sometimes the Chinese characters will be displayed as garbled?
A: This problem is usually related to the implementation of each JDBC Driver. At present, most JDBC Driver uses local encoding format to transmit Chinese characters, such as Chinese characters "0x4175" will be transferred to "0x41" and "0x75". Therefore We need to convert the characters returned by JDBC Driver and the characters to be sent to JDBC Driver.
When inserting data into the database with JDBC Driver, you need to transfer Unicode to Native Code; when JDBC Driver 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 ) {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 the above two methods, some JDBC Driver If the correct character set attribute is set to JDBC Driver Manager, the above 2 methods are not required.
Q: When using servlet to process HTTP requests and generate returned HTML pages, how to make the Chinese characters in the HTML page can display normal? A: javax.servlet.http.httpResponse class is used to generate return pages. Method defined by httpresponse GetOutputStream () You can get an instance of servletOutputStream so that users can write to the output stream to return to the output stream using the servletoutPutStream.Write method. But servletOutputStream uses the default encoding method, if you want to make the return page in the page Normally, it is best to specify the character encoding method used. It is often necessary to construct an OutputStreamWriter, the routine is as 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 Web Server to include the user's class file?
A: There are two ways to 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 Web Server starts, the class of the classes directory is Automatically add the ClassPath environment variable. Modify the httpd.nojre file, add the path name where the user class file is added to the ClassPath environment variable.
Q: Why use naming.lookup on a Windows platform to get a remote RMI object?
A: The machine's network settings are incorrectly probably caused. RMI uses Java network classes, especially Java.Net.Netaddress classes, which will query TCP / IP host names, including IP addresses to host names Mapping and host names to 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. The delay you find is the delay of the DNS query. Please try to add all hostname / IP addresses involved in RMI communication to Local files WinntSystem32DriverseetChosts or Windowshosts. The format is as follows:
IP address host name
This setting should significantly reduce the time spending the query.
Q: How to set up proxy information when writing Java Application to access external websites?
A: If you access the external website in the Java Application, you should first set up 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); connection = (httpurlconnection) u.openConnection (); ..... Q: Swing component jlist The list data is modified, how do I notify the JList change display?
A: The JList component has a separate display mode ListModel to represent the 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 is observed in its data mode ListModel. Therefore, the correct implementation of a ListModel should notify the event's listener when each data is changed. When using the constructor jlist (Object []), the system will automatically create an instance of a defaultListModel to store jlist. Display data, you can call DEFAULTLISTMODEL to dynamically modify JLIST data, such as RemoveElementat (INDEX), AdDelement (Object), etc. DEFAULTLISTMODEL will notify Jlist to change the data.
Q: How do I implement a pattern dialog in the Java applet?
A: The key to implementing the mode dialog box in the Java applet is to specify a correct parent window for the dialog when creating a dialog. Because Applet is a subclass of the Panel class, it is not a parent window as a dialog. So first get the window where Applet is located, as 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 instanceof frame) return (frame) C; c = c.getParent ();} return null;}
Q: How do I display another HTML page in the Java Applet?
A: The appletContext () method can get the browser in which the applet is located in the browser where the applet is configured by appletContext () method can get the browser in which the applet is located.
Q: Can I run in Netscape or IE with a signature applet implemented by JDK?
A: The signature applet implemented with JDK cannot be run in Netscape or IE, but can be 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 JDK's signature Applets to run in Netscape or IE, the solution is to install Java Plugin in Netscape or IE, and the signature applet implemented with JDK can run in both browsers. Q: Use JNI Technologies can call the C library from the Java application, but how to make the C library to call additional C libraries?
A: If a C library C1 called by Java still needs to call another C library C2, then the library C2 should be coupled when compiling C1, the steps are as follows (Solaris Platform): Write the Java file calling the C library, and Compilation. Javac Java file name
Generate C program header file Javah -jni java file name (without suffix .java)
Written the C program C1.c called by Java, as well as C2.c called C1, compiled. Cc -g -iinclude path name c2.c-l libc2.so cc-iinclude 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 all drive names in the PC file system in the Java language?
A: In Java 2, the File class in the java.io package adds method Listroots () to implement this feature.
Q: Why is Runtime.exec ("LS") without any output?
A: Call the runtime.exec method will generate a local process and return an instance of a Process subclass that can be used to control the process or obtain the process. Due to the child process created by the runtime.exec method. Terminal or console, the standard IO of the sub-process (such as stdin, stdou, stderr) is redirected by process.getoutputStream (), process.getinputstream (), process.GeterrorStream () method is redirected to its parent process. User These streams need to be used to enter data or get the output of the sub-process to the child process. So correct the runtime.exec ("ls") of Runtime.exec ("LS") is as follows:
try {process = Runtime.getRuntime () 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 ("ooException" E.getMessage ());}
Q: How do I 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 keys, private keys, certificates, and signature JAR files. For details, please refer: http://java.sun.com/security/usingjavakey.html and Java 2 The signature mechanism has been made more improved, allowing users to set security more flexibly. Java 2 provides three tools: KeyTool, PolicyTool, and Jarsigner to implement signing applet. For example, Joe has written a signature applet: Signedapplet.java, The process of generating a simple signature applet is as follows:
/ / Generate a key, the key is not named Joe, the password is SIGN12, stored in keytool -genkey -alias joe-ready-gene 12 -keystore joestore // in the keystore Joestore, Package SignedApplet.class and related files into JAR file JAR CVF SignedAppletDemo.jar // using self-signed certificates generated with keytool generate the signature applet (jar file) jarsigner -keystore joestore -signedjar joe.jar SignedAppletDemo.jar joe // output from the self-signed certificate to the keystore file keytool -export -keystore Joestore -Alaias Joe -File Joe.cer, for the signature applet acceptor Susan, you need to securely perform the Joe written by Joe by the following steps to securely perform JOE's certificate and read it into the SusanStore in the keystore. Keytool-Icr -Alaias Joe -file Joe.cer -keystore SusanStore // Run PolicyTool Generate Policy file policytool // runs with AppletViewer, or install Java Plugin in the browser. About signing applet in Java For deployment in Plugin, please refer to the following webpage:
http://java.sun.com/security/signexample12/
Note: The above example is simply, using the self-signed certificate produced by KeyTool. In fact, users can
Use KeyTool-Certreq to apply for an electronic certificate to the commercial CA center.
Q: If the ObjectOutputStream is written to the Object multiple times in a file, why use ObjectInputStream to read these Object When you read these Objects, streamcorruptedException?
A: When using the default Serialization implementation, an ObjectOutputStream constructor and an ObjectInputStream constructor must correspond to the constructor of the enabjectOutputStream written to the output stream, while ObjectInputStream will first read this identity head. Therefore, when writing to an Object in a file multiple times, the file will contain multiple identity heads. So use ObjectInputStream to deserialize this ObjectOutstream, a STREAMCORRUPTEDEXCEPTION is produced. One solution is to construct an ObjectOutputStream child Class, and override the WriteStreamHeader () method. The overridden WriteStreamHeader () method should determine if it is written to Object in the file, carbonyl? Call super.writestreamheader (); if, ie, when writing Object The ObjectOutputStream.Reset () method should be called.
Q: The serialization class is a flow-oriented, how should I write objects in 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, and ObjectInputStream 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: You can. Java.rmi package provides class java.rmi.registry.locateRegistry, used to get the name service or Create a name service. Call the locateregistry.createRegistry (int port) method You 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 get the name service.
Q: How should I set print attributes such as printer names using class printjob
A: Use the following method to get the sample of PrintJob to control the print operation:
Toolkit.GetPrintJob (Frame F, String Jobtitle, Properties Prop)
Then the settings for printing properties can be implemented by setting the Prop properties, print properties include:
AWT.PRINT.DESTINATION: Can be "printer" or "file"
AWT.PRINT.PRINTER: Printer Name
AWT.PRINT.FILENAME: Print file name
AWT.PRINT.NUMCOPIES: print
AWT.PRINT.OPTIONS: Print Options for Print Commands
AWT.PRINT.ORIATATION: Printing Direction, can be "portrait" or "landscape"
AWT.PRINT.PAPERSIZE: Paper size, can be "letter", "legal", "executive" or "a4"
Q: The thread class in JDK1.1 defines the suspend () and resume () methods, but it is already in JDK1.2, what method should be used instead?
A: Thread.suspend itself is easy to generate a deadlock. If a target thread is connected to a key system resource, then the thread is Suspend, then other threads will not access the system resource unless the thread is resume. If another thread will call Resume so that the thread continues to run, before this, it also needs to access this system resource, will result in a deadlock.
Therefore, in Java 2, the plurality of popular methods is to define the status variable of the thread and poll the target thread. When the status is a suspension state, it is necessary to use the wait () method to wait for the waiting state. Once it is required The thread continues to run, and other threads will call the Notify () method to inform it.
Q: Use JDBC programming, how to control the results set Resultset's pointer, allow it to move up and down, and move to the first row of the result set and the last line?
A: In JDK1.1, only the next () method is defined in the ResultSet class to support the down movement of the data pointer. However, in Java 2, the ResultSet class adds the following method to support the movement of the data pointer, including:
ResultSet.First (): Move the data pointer to the first line of the result set
ResultSet.last (): Move the data pointer to the last line of the result set
ResultSet.PRevious (): Move the data pointer
The above methods are defined in the JDBC2.0 specification, all JDBC drivers that support JDBC 2.0 can support the above methods. The JDBC driver manufacturers such as INTERSOLV and OPENLINK are currently supported by JDBC 2.0.
Q: Which Web Server supports servlet? How to make IIS support servlet?
A: Currently, Servlet servers mainly include: Sun's Java Webserver, Lotus Domino Webserver, BEA WebLogic TenGah Server, Jigsaw, Netforge, Acmeserver, and Mot Bays Jetty, etc.
In addition, some third-party manufacturers have also developed Servlet Engine to enable other WebServer (such as Netscape Web Server, IIS, etc.) to run servlet, such as LiveSoftware's JRun (http://www.livesoftware.com/ Products / JRUN /), etc. Q: How do I store the image into the image file in the Java application?
A: Java Advanced Imaging API (included in Java Media API) Allows complex, high-performance image processing in Java applications. Jai API provides the ability to store images. Currently, JAI API supports the following image file formats: BMP, JEPG, PNG, PNM, TIFF. A piece of code stored in the BMP file below:
OutputStream os = new FileOutputStream (fileToWriteTo); BMPEncodeParam param = new BMPEncodeParam (); ImageEncoder enc = ImageCodec.createImageEncoder ( "BMP", os, param); enc.encode (img); os.close ();
For a programming guide for storing image files, please refer to the following webpage:
Http://java.sun.com/products/java-media/jai/fordevelopers/jai-guide/
Q: How to read and write data to serial port in Java language? Font>
A: Sun's Java Communication API2.0 can be used to read and write serial ports that support RS232 serial port and IEEE 1284 and provide a string / parallel communication mechanism that is unrelated to the platform.