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: SETENV JAVA_HOME JAVA2 installation path
SetENV PATH $ java_home / bin: $ {pat}
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 compatible with JDK. At this point, the solution can be Java Plugin using the Hotjava browser or installing Sun.
If an error occurs in the HotJava browser or AppletViewer, you should check the Applet program based on the error prompt.
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.
ask:
When using servlets to process HTTP requests and generate returned HTML pages, how to make Chinese characters in the HTML page normal display?
answer:
javax.servlet.http.HttpResponse class used to generate the page returned by the getOutputStream method () can be obtained Examples of ServletOutputStream HttpResponse defined, so that the user can return the content of the page is written to the output stream using a method ServletOutputStream.write However ServletOutputStream Using the default encoding method, if you want to make the Chinese characters in the return page, it is best to specify the character encoding method used. It is often necessary to construct an OutputStreamWriter, the routines are as follows:
Public void doget (httpservletRequest Req, httpservletResponse res)
Throws ServleTexception, IOException
{
Res.SetContentType ("text / html");
ServletOutputStream out = res. maxOutputStream ();
OutputStreamWriter OW = New OutputStreamWriter (OUT, "GB2312");
OW.WRITE ("This is the test");
Ow.flush ();
OW.CLOSE ();
}
ask:
How do I set the ClassPath of Java Web Server to include the user's class file?
answer:
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 Webserver starts, the class directory is automatically added to the ClassPath environment variable.
Modify the httpd.nojre file to add the path name of the user class file to the ClassPath environment variable.
ask:
Why use naming.lookup on a Windows platform to get a remote RMI object?
answer:
The machine's network settings are incorrectly probably caused by this problem.
RMI uses Java network classes, especially Java.Net.ineTaddress classes, which will query the host name of TCP / IP, including the IP address to the host name mapping and host name to the IP address. In the Windows platform, this query The 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?
answer:
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;
CONNECT = (httpurlconnection) u.openConnection ();
.....
Q: The list of Swing Components Jlist is modified, how to inform JList change display? A:
The JList component has a separate display mode ListModel to represent the display data of JLIST.
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 notify the listener of the event when each data is changed.
When you create a JList (Object []) instance, the system will automatically create an instance of the DefaultListModel to store JLIST display data, you can call the simple way defined in DEFAULTLISTMODEL to dynamically modify the JList data, such as RemoveElementat. (INDEX), addelement (Object), etc. DEFAULTLISTMODEL will notify Jlist to change the data on data while modifying data.
ask:
How to 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 Applet is a subclass of the Panel class, you can't use the parent window of the dialog, so first To get the window where the applet is located, the parent window of the Mode dialog. The sample code is as follows:
.....
Dialog D = New Dialog (getParentWindow (Comp), Title
/ / CoMP is any component on 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 do I display another HTML page in the Java Applet?
answer:
With the Java.applet.applet.getAppletContext () method, you can get the appletContext, AppletContext.ShowDocument (URL) method to display another web page in the browser where the applet is located.
ask:
Use the JDK-implemented signature applet, can you run in Netscape or IE?
answer:
The signature applet implemented with JDK cannot be run in Netscape or IE, but you 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 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.
ask:
With JNI technology, you can call the C library from the Java application, but how to make the C library can call another C library?
answer:
If a C library C1 called by Java still needs to 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 and compile.
Javac Java file name
Generate C program head file
Javah -jni java file name (without suffix .java)
Written the C program C1.c called by Java, and C2.c called C1, and compile.
Cc -g -iinclude path name c2.c -o libc2.so
CC-INCLUDE path name -lc2 c1.c -o libc1.so
Set an environment variable
Setenv LD_Library_Path Libc1.so, Libc2.so location path: $ {ld_library_path}
Run Java application
ask:
How to list all drive names in the PC file system in the Java language?
answer:
In Java 2 versions, the File class in the java.io package adds a new method Listroots () to implement this feature.
ask:
Why does Runtime.exec ("ls") do not have any output?
answer:
Calling 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, there is no end or The console, so the standard IO of the sub-process (such as stdin, stdou, stderr) is redirected to its parent process through process.getoutputStream (), process.getinputstream (), process.GeterrorStream () method. User needs to use These streams come to the child process or get the output of 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 iexception e) {
System.err.Println ("IOException" E.getMessage ());
}
ask:
How to generate a signature applet so that the applet can access local resources?
answer:
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 pair signature The mechanism has made a relatively large improvement, 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 app: Signedapplet.java, then generate A simple signature applet is as follows:
/ / Generate a key, the key is not named Joe, the password is SIGN12, stored in the keystore joesteore
Keytool -Genkey -Alaias Joe -Keypass Sign12-KeyStore Joestore
// Package SignedApplet.class and related files into JAR files
JAR CVF SIGNEDAPPLETDEMO.jar
// Use the self-signed certificate generated by KeyTool to generate the signature applet (JAR file)
Jarsigner -keystore joesteore -signedjar joe.jar SignedappletDemo.jar Joe
// Out the self-signed certificate from the keystore to the file
KeyTool -Export-Keystore Joestore -Alias Joe -file Joe.cer
For the acceptor Susan for signing Applet, you need to be safely executed by the following steps.
Joe written signature applet:
// Get Joe's certificate and read it into the SusanStore in the keystore
KeyTool -Import -Alaias Joe -file Joe.cer-Keystore SusanStore
// Run PolicyTool to produce Policy files that meets Susan requirements PolicyTool
// Run it with AppletViewer or install Java Plugin in your browser.
For the deployment of the signature applet in Java 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.
ask:
If the ObjectOutputStream is written to an additional manner multiple times in a file, why is StreamCorruptedException when reading these objectIntNPutStream?
answer:
When using the default serialize, a constructor of an ObjectOutputStream and an ObjectInputStream constructor must correspond to the constructor of the ObjectOutputStream written to the output stream, and ObjectInputStream will first read this identity head. Therefore, When writing Object in a file multiple times, the file will contain multiple identity heads. So use ObjectInputStream to deserialize this ObjectOutputStream, a solution is a solution that can construct an ObjectOutputStream subclass, And override the WriteStreamHeader () method. The written WriteStreamHeader () method should determine whether to write to Object in the file for the first time, carbonyl? When you call Super.WriteStreamHeader (); if, ie, when writing Object, The ObjectOutputStream.Reset () method should be called.
ask:
The sequence of objects is a streaming, how should you write objects to a random access file?
answer:
Currently, there is no direct method to write objects into a random access file.
However, you can use the ByTearRay input / output stream as an intermedion to write or read bytes from the random access file, and can create object input / output flows for reading by using byte flow. Write objects. Need not to note that in the byte stream, you want to include a complete object, otherwise an error will occur when you read and write objects. The random access file can be written directly. In contrast, we can read the byte array from a random access file, using it to construct ByteArrayInputStream, which in turn construct ObjectInputStream to read objects.
ask:
When running the RMI application, you can not start the name service RMIREGISTRY, but start it from the program?
answer:
Yes. Java.rmi.Registry.locateREGISTRY is provided in the java.rmi package to obtain a name service or create a name service. Call the locateregistry.createRegistry (int port) method to create a name service at a particular port, so that users do not need Start RMIREGISTRY manually. In addition, LocateRegistry.getRegistry (String Host, int port) method can get the name service.
ask:
How should print an printed attributes such as printer names when printing using class printjob
answer:
Use the following method to get the sample of PrintJob for controlling the printing 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: Costs 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"
ask:
In JDK1.1, the thread class defines the suspend () and resume () methods, but what methods should be used in JDK1.2?
answer:
Thread.suspend itself is easy to generate a deadlock. If a target thread is locked for a key system resource, then the thread is SUSPEND, then other threads will not be able to access the system resources unless the thread is resume. If Another thread will call the Resume so that the thread continues to run, and 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.
ask:
Using JDBC programming, how to control the results set ResultSet, so that it can be moved up and down, and move to the first row and the last line of the result set?
answer:
In JDK1.1, the next () method is defined in the ResultSet class to support the down movement of the data pointer. But 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.
ask:
Which Web Server supports servlet? How to make IIS support servlet?
answer:
Currently, Servlet servers mainly include: Sun's Java Webserver, Lotus Domino Web Server, BEA WebLogic TenGah Server, Jigsaw, Netforge, Acmeserver, and Mot Bays Jetty.
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. .
ask:
How do I store images into the image file in the Java application?
answer:
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 (Filetowritto);
BMPENCODEPARAM PARAM = New BMPENCODEPARAM ();
ImageCoder 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/fordevelop/jai-guide/ Q:
How to read and write data to serial port in Java language? Font>
answer:
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.
For detailed documentation, please visit: http://java.sun.com/products/javacomm/
Welcome to reprint in the case of retaining http://www.javajia.com!