Java FAQ highlights

Source: Internet
Author: User
Tags driver manager
Java FAQ highlights-general Linux technology-Linux programming and kernel information. The following is a detailed description. Q:
How to set the environment variables of Java 2 (JDK1.2?

A:
After installing Java 2, 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 for the system class library and the current PATH of the user.

Java 2 environment variables are set as follows:

Solaris platform: installation path of setenv JAVA_HOME Java2

Setenv PATH $ JAVA_HOME/bin :$ {PATH}

Windows: installation path of set JAVA_HOME = Java2

Set PATH = $ JAVA_HOMEbin; % PATH %

Q: Which Java integrated development tools support Java 2?

A:
Currently, popular Java integrated development environments, such as Inprise JBuilder, Symantec's Visual Cafe, and Sybase PowerJ, all support Java 2.

Q:
If an error occurs when running a Java applet in Netscape or IE, how can I determine the error range?

A:
When java applet runs in the browser, the default JVM of the browser is used. different browsers have different levels of support for JDK. therefore, an error occurs when running a Java applet in Netscape or IE. We recommend that you use the JDK tool appletviewer or Sun's Hotjava browser to test the applet, to determine whether the error is caused by a browser.

If the applet runs normally in appletviewer or Hotjava, the error occurs because the browser is not fully compatible with JDK. the solution can be to use the Hotjava browser or install Sun's Java Plugin.

If the applet runs in the Hotjava browser or appletviewer, an error occurs. Check the applet according to the error prompt.

Q:
Why sometimes Chinese characters are garbled when JDBC is used to insert data into or extract data from the database?

A:
The implementation of this problem is usually related to the implementation of each JDBC driver. currently, most JDBC drivers use local encoding formats to transmit Chinese characters. For example, Chinese characters "0x4175" are converted to "0x41" and "0x75" for transmission. therefore, we need to convert the characters returned by the JDBC driver and the characters to be sent to the JDBC driver.

When you use JDBC driver to insert data to a database, you need to convert Unicode to native code first. When the JDBC driver queries data from the database, you need to convert the native code to Unicode. the implementation of these two conversions is given below:


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 preceding two methods, some JDBC drivers do not need to set correct character set attributes for jdbc driver Manager.


Q:
When Servlet is used to process http requests and generate returned HTML pages, how can the Chinese characters in the HTML pages be properly displayed?

A:
Javax. servlet. http. the HttpResponse class is used to generate a response page. you can use the getOutputStream () method defined by HttpResponse to obtain the instance of ServletOutputStream, so that you can use ServletOutputStream. the write method writes the content of the returned page to the output stream. however, ServletOutputStream uses the default encoding method. To enable the normal display of Chinese characters on the returned page, it is best to specify the encoding method. generally, you need 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 a test ");

Ow. flush ();

Ow. close ();

}


Q:
How to set the Java WebServer CLASSPATH to include the user's class file?

A:
You can set the CLASSPATH environment variable of Java WebServer in two ways, so that the Servlet compiled by the user can call the class file of the user.
Put your class file in the JavaWebServer_Dir/classes directory. When Java WebServer is started, the classes directory is automatically added to the CLASSPATH environment variable.
Modify the httpd. nojre file and add the path name of the user class file to the CLASSPATH environment variable.


Q:
Why is it slow to obtain remote RMI objects using Naming. lookup on Windows?

A:
An incorrect network configuration may cause this problem.
RMI uses Java Network classes, especially java.net. inetAddress class, which queries the TCP/IP host name, including IP address-to-host ing and host-to-IP address ing. on Windows, this query function is implemented by the local Windows Socket library. therefore, the latency occurs in the Windows library, not in RMI.

If your machine is set to use DNS, the problem is that the DNS server cannot find the host name, And the latency you find is the latency of DNS query. add all host names/IP addresses involved in RMI communication to the local file winntsystem32driversetchosts or windowshosts. the format is as follows:

IP address Host Name

This setting should significantly reduce the query time.

Q: How do I set proxy information when writing a Java application to access an external website?

A:
If you access an external website in java application, set the proxy information first. 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) u. openConnection ();

.....

Q: The JList data of the Swing component has been modified. How can I notify the JList to change the display?

A:
The JList component has a separate display mode ListModel to represent the JList display data.
After JList is created, the value of JList data elements and the number of data elements can be dynamically changed.
JList observes data changes in its data mode ListModel. Therefore, the correct implementation of a ListModel should notify the event listener every time the data changes.
When you use the constructor JList (Object []) to create a JList instance, the system automatically creates an DefaultListModel instance to store the display data of JList, you can call the simple method defined in defalistlistmodel to dynamically modify JList data, such as removeElementAt (index) and addElement (Object. defaultListModel notifies the JList of data changes while modifying data.

Q:
How to implement a mode dialog box in Java applet?

A:
The key to implementing the mode dialog box in Java applet is to specify a correct parent window for the dialog box when creating a dialog box. because the Applet is a subclass of the Panel class and cannot be used as the parent window of the dialog box, you must first obtain the window of the applet as the parent window of the mode dialog box. the sample code is as follows:


.....

Dialog d = new Dialog (getParentWindow (comp), title );

// Comp is any component of the applet.

....



Public void getParentWindow (Component compOnApplet, String title ){

Container c = compOnApplet. getParent ();

While (c! = Null ){

If (c instanceof Frame)

Return (Frame) c;

C = c. getParent ();

}

Return null;

}

Q: How can I display another HTML page in a Java applet?

A:
The AppletContext and AppletContext. showDocument (URL) methods related to the applet can be obtained through the java. Applet. applet. getAppletContext () method to display another webpage in the browser where the applet is located.

Q:
Can I run the signature applet implemented by JDK in Netscape or IE?

A:
The signature applet implemented by JDK cannot be run in Netscape or IE, but can be run in Hotjava browser.

Different browsers provide different signature applet mechanisms. For example, Netscape provides the zigbert tool and Capability API, while IE uses the CAB file. however, neither the signature applet generated by the Netscape tool nor the signature applet generated by IE can be run in other browsers.

To enable the JDK signature applet to run in Netscape or IE, install Java Plugin in Netscape or IE, then, the signature applet implemented by JDK can be run in these two browsers.

Q:
The JNI technology can be used to call the C library from a Java application. But how can this C library call another C library?

A:
If a C library C1 called by Java still needs to call another C library C2, it should be connected to library C2 when compiling C1. The steps are as follows (Solaris platform ):
Compile and compile the Java file that calls the C library.
Javac java file name


Generate C program header file
Javah-jni java file name (without the suffix. java)


Compile and compile the C program C1.c called by Java and C2.c called by C1.
Cc-G-Iinclude pathname C2.c-o libC2.so
Cc-G-Iinclude pathname-lC2 C1.c-o libC1.so


Set Environment Variables
Setenv LD_LIBRARY_PATH libC1.so, path of libC2.so
:$ {LD_LIBRARY_PATH}


Run a java application


Q:
In Java, how do I list all the drive names in the PC file system?

A:
In Java 2, the File class in the java. io package adds the listRoots () method to implement this function.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.