Java FAQ (2)

Source: Internet
Author: User

 

Ii. Commands

Q2.1 I wrote the first Java program. How should I compile/run it?
A: First, save the program as a XXX. Java file. Note that you may need to modify the file suffix. Then use javac XXX in the DOS window. java command, you will find that there is another XXX in this directory. class file, and then use the Java XXX command to start running your Java program.

Q2.2 I did as you said, but what "'javac' is not an internal or external command, nor a runable program or a batch file .".
A: You have encountered a path problem. The operating system searches for javac.exe in a specific dimension (path.exe, but cannot be found.

Edit the environment variable of your operating system, add a java_home variable, set it to your JDK installation directory, edit the PATH variable, and add % java_home %/bin. Save and open a new DOS window, and you can use javac and Java commands.

Q2.3 how to set environment variables?
A: Please contact someone else.

Q2.4 what "unreported exception java. Io. ioexception;" is displayed during javac XXX. Java ;".

A: see q4.8 to learn about the exception mechanism in Java.

Q2.5 javac XXX. Java passed smoothly, but what "noclassdeffounderror" is displayed in Java XXX ".

A: 1. You have encountered the classpath problem. The Java command searches for class files that you directly or indirectly use within a certain range (classpath), but cannot be found. First, make sure that you have not typed it into Java XXX. second, check your classpath environment variable. In fact, you do not need to set this variable, but if you set this variable, it does not contain. (representing the current directory), you will encounter this problem. Add a variable to your classpath environment variable or simply delete the variable.

2. If you use a standard package that is not included in the JDK, such as the javax. servlet. * package, you will also encounter this problem. Add the corresponding JAR file to the classpath.

3. if you define a package in the Java source file, see q2.11.

Q2.6 I displayed "exception in thread" Main "Java. Lang. nosuchmethoderror: Main" in Java XXX ".

A: First, each Java file in your program has only one public class. The class name must be exactly the same as the file name. Second, there is only one public static void main (string [] ARGs) method in the class you want to run. This method is your main program.

Q2.7 "exception in thread" Main "Java. Lang. nullpointerexception" is displayed in Java XXX ".

A: In a program, you try to call a method on an object variable with a null value. Check your program to ensure that your object is properly initialized.
See q4.8 to learn about the exception mechanism in Java.

Q2.8 what does package mean? How to use it?

A: to uniquely identify and group each class, Java uses the Package concept. Each class has a full name. For example, the full name of string is Java. Lang. String, where java. Lang is the package name and string is the short name. According to Java Naming Conventions, the package names are all in lower case, while the first letter of the class name is in upper case. In this way, if you define the class string with the same name, you can put it in mypackage and distinguish the two classes by using the full name mypackage. String and Java. Lang. String. At the same time, you can put the logic-related classes in the same package to make the program structure clearer. To define a package, you must add a line "package mypackage;" at the beginning of the Java file ;". Note that the package is not nested or contains a relationship. The mypackage package and mypackage. mysubpackage are two parallel packages for the JRE (although the developer may imply the inclusion relationship ).

Q2.9 what if I didn't declare any package?

A: Your class is considered to be in the default package. The full and short names are the same.

Q2.10 how to use other classes in one class?

A: If you use a java. lang package or a class in the default package, you do not need to do anything. If your class is in the mypackage and you want to call other classes in the same package, you do not have to do anything. If you use classes in other packages, after the package declaration, before the class declaration, use import otherpackage1.class1; or import otherpackage2 .*;
Here. * Indicates introducing all classes in this package. Then you can use the short names of other classes in the program. If there is a duplicate Name Conflict between short names, you must use the full name to distinguish them.
Note that when using classes in other packages, you can only use public classes and interfaces. For more information, see q5.4.

Q2.11 "noclassdeffounderror" is displayed when I use a package, but it works normally when I remove all packages.

A: Save your java files by package name. For example, if your working directory is/work and your class is package1.class1, store it as/work/package1
/Class1.java. If no package is declared, put it directly under/work. Run javac package1/class1.java under/work and then Java package1.class1. You will find that
Everything works. In addition, if you have more classes and need to use packages for organization, you can consider using IDE.

Q2.12 What should I do if I want to compile Java into an EXE file?

A: JDK can only compile Java source files into class files. A class file is a cross-platform bytecode that must be run based on the platform-related JRE. Java to achieve cross-region
Platform. Some development tools can compile Java files into EXE files. The author objected to this practice because cross-platform is canceled.
If you are sure that your software only runs on Windows, you can use C ++/C # for programming.

Q2.13 what does "deprecated API" occur during compilation?

A: deprecated refers to a method that is out of date but is retained for the sake of forward compatibility. These methods may be removed later. You should use a newer method. In the API, it will show you how to replace it.

 

From: http://www.cndw.com/tech/page/200602154469.asp

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.