1: About setting ClassPath:
1) General .jar files are placed in the lib directory. The .class file is placed in the class directory. .java files in the src directory.
2) ClassPath can be set in the ClassPath in an environment variable. You can also use the javac -classpath command parameter setting, one note: Use the javac -classpath command parameter to overwrite the ClassPath in the environment variable. Unless you add% classpath%, it is best to restart after changing the classpath (generally log out, or turn off the CMD, and then open).
For example: java -classpath% classpath%; .; d: /myjava/myjade/lib/jade.jar;
Here. Represents the current directory, .. Indicates the parent directory.
The specific information can be viewed by adding a command parameter - Verbose,
Such as: Java -verbose Hello, Javac -verbose Hello.java.
3) In one .java file, if there is a package command line, then set the classpath to Package. Such as: package example.ping, Examples / PingAgent Under d: / myjava / myjade, set ClassPath to d: / myjava / myjade, do not have to add Examples.PingAgent. If it is a JAR package, you need to refer to the name of the JAR package. Set ClassPath to d: /myjava/myjade/mypackage.jar.
4) If prompted information prompts Bad Major Version Number. It may be that there are multiple versions of JDK (SDK) on the machine.
And execute the bottom version of JDK. Use the java -version command to view the currently executed JDK version. Putting the high version of% javahome% / bin in the Path path.
Note: These are experienced on the Windows machine, because Windows has a registry, so some default settings.
It is not possible under Linux.
5) On the command line, click the Java command to display all command parameters.
6) About packaging, use WinRAR to compress into. JAR files are not available, need to provide JAR commands provided by JDK. How to use
Set in the E: / myApp / src there is a zfy directory, there is a subdirectory in the zfy directory also have Java files.
If you want to pack the zfy directory, the package name my.jar (the package specified by the java impRot command is not related to the JAR package name. As long as the name included in JAR is correct. If ZFY is a package, it can be knocked down my.jar Medium, does not affect the use)
Enter JAR CVF my.jar zfy under the command line prompt E: / myApp / src>
If you want to pack all things under SRC, enter JAR CVF my.jar *
7) Search order for Java class:
When executing the Java and Javac commands, the Java fundamental class (自 自) (自 举: rt.jar, sunrsasign.jar, jsse.jar, jce.jar, using the java -verbose command to see Open class) has the default search path: first Search the class files under the parent directory of Java and Javac in the parent directory of the JRE directory, both of JDK's JRE / LIB. If not, search the lib directory in the specified Java Runtime Enviroment Java running environment in the registry (here is C: / Program Files / Java / J2RE1.4.1_01 / LIB).
So the classpath should be specified should be the user's own class and package, and some Java basic classes do not include the class, and the general JRE / LIB includes the category without specifying. You can specify Tools.jar and Dt.jar in java_home / lib as specified in ClassPath. Tools.jar includes the source code for the bin directory, without Tools.jar, the javac command cannot run. Do not specify Tools.jar in ClassPath, Java can also be found by default. Use the java -verbose> info.txt command to output the output content to the info.txt file
2: Variable (handle)
When you create a handle, be initialized.
Common variables are created into the stack (RAM, CPU management): char x = 'a'; so access is fast.
Java classes are created in a heap (RAM, CPU]: Character C = New Character ('a');
If the field of the variable is class, Java automatically sets its default. If it is a variable in the method, no matter, manual initialization is required.
So it is best to initialize all variables.
3: About javac, javadoc command
If the original file is placed under the SRC folder, you want to put the compiled .class file in the Classes folder (both in the E: / MyApp), use the -d parameter when using the javac command,
Such as: javac -d / myapp / classes myclass.java
Use the javadoc command to easily generate the HTML help documentation of the .java file, put it under the DOC directory.
Such as: javadoc -d / myapp / doc myclass.java
4 Perform files in the package
Such a folder in E: / Download / XML / Tools / JavaXML / CH10: com / oreilly / xml, there is such file LightweightxmlrpcServer.java.
The whole path of the document is: E: / Download / XML / Tools / Javaxml / Ch10 / Com / OREILLY / XML / LightWeightXmlrpcServer.java.
There is a package com.ioLly.xml line in this file, which specifies the package of the file.
If you want to perform this class, you need to compile this: First enter the E: / Download / XML / Tools / Javaxml / Ch10
Executive: javac com / oreilly / xml / lightweightxmlrpcserver.java
Executive: javac com.ioLly.xml.lightweightxmlrpcserver [args]
Some other Java compile commands:
1) Javac-ClassPath LiB / Axis.jar; lib / jaxrpc.jar localhost / axis / hello_jws / *. Java
2) java -djava.ext.dirs = lib -cp. Localhost.axis.hello_jws.main
5 correctly use "package"
In the process of using the package, there are many small details that need attention, and common problems are listed below:
1. There are usually two ways to set the class path:
I) Set in the environmental variable of the system, set the method according to the platform;
II) Set in the form of command parameters.
Such as: Javac-ClassPath D: /jdk1.4.2/lib d: /cjm/edu/test/testfile.java java -classpath.; D: /jdk1.4.2/lib; d: / cjm edu.test .Testfile Note: i) The Javac and Java command have great differences. You can distinguish it. Javac is a platform command that manifests the specific platform files to indicate the compiled file path. Java is a virtual machine command, which is a description of the class, that is, the description of the class, and cannot add the extension, pay attention to the case of the class name.
II) There is a very strange question, that is, the ClassPath behind the javac command contains the current directory (conforming to Windows habits), but the classpath behind the java command does not contain the current directory, so it must not forget in its classpath. Plus the description of the current directory, add ".".
2, use "." To be "." In the Java program, and also have the concept of the current directory. To run the testfile in Figure 1 must be specified as edu.test.testfile. But if you want to call and it in the class testfile, you don't have to specify the directory prefix.
3. All the categories used in the Java program should clearly indicate the search path of this class. There are generally two ways to indicate:
I) Indicate the Import keyword in the beginning of the program. If you want to use the FileInputStream class in the class Testfile, add import java.io.fileReader in the program head; or import java.io. *;
II) Write the full path directly to the FileReader class, such as: java.io.fileReader fin = new java.io.fileReader ("filename");
Note: Java.lang package is always imported by default.
4, the class directory structure must be consistent with the first "packet declaration" in the class. If the class testfile.class corresponds to the first sentence of the Java file, you must include: package edu.test;
Make sure that the type of storage path and "package path" indicated in the class generally have two:
j 在
Javac-ClassPath D: /jdk1.4.2/lib d: /cjm/edu/test/testfile.java After the compilation is complete, the descriptive path generated in the Java file in the compile command will appear. That is, in D: / Test / EDU / TEST
II) Compile the program through the use of the -D parameter. If you use the following statement to compile:
Javac-D: / CJM D: /TEMP/TestFile.java will automatically create a directory according to the directory structure specified in Packagek after -d, and will result in .class. The file is placed in this new directory, that is, set the / edu / test directory below D: / CJM, then generated TestFile.class placed in the D: / CJM / EDU / TEST directory.
5. In order to facilitate the project release, you can make your own tree into .jar files. If all types of files below the EDU in Figure 1. You can go to the D: / CJM directory first, then use the following command:
Jar -cvf test.jar EDU / now generate a Test.jar file under D: / Test, this .jar file contains the full directory structure and files under EDU. When using this .jar file, simply specify the .jar file in the ClassPath. 6, the use of other resources, such as icon files, text and other resource files must be noted that the finding resource file should not start from the directory where the class file is located, but should start from the starting point of the class path specified by package (Figure 1 Start from the catalog where the EDU is located. As seen in Figure 1, Word.txt is under Resource, and the class file testfile.class Under EDU / Test, you want to use Word.txt in Resource in TestFile.class, you want to follow:
Fin = New FileReader ("resource / word.txt"); not: fin = new fileReader ("../../ resource / word.txt");