Compile and run Java programs by yourself

Source: Internet
Author: User

I am a beginner in Java. When I see other people say that I should not use development tools such as Eclipse and netbeans before learning Java, so I will try it on my own. There are a lot of mistakes made in the process. Refer to the experiment of Li Gang's crazy Java 1.1 point. since you have used makefile, you have used makefile for an experiment. At last, you can compile and run some small files without using eclipse or netbeans.ProgramThe following is my summary. If you have any mistakes, please correct them. Thank you.

This article is divided into four sections:

Section 1: configure the Java Runtime Environment
Section 2:CodeCompilation and running
Section 3: how to pack a jar package and use a jar package
Section 4: use makefile to automatically compile and run Java programs

Section 1: configure the Java Runtime Environment

This section uses Windows XP as an example to configure the Java Runtime Environment. This section is very simple. Let me make a short speech.

1: Download the JDK Development Kit, decompress it, and install it.
2: add the PATH variable to the user environment variable (you can add it to the system environment variable, but you may need to restart the operating system) and set the value.

For example, if I install JDK in the F: \ Program \ Java directory, set the PATH variable to F: \ Program \ Java \ JDK \ Program ..... Then we can use javac, Java, jar and other commands in cmd.

For example, if you run the Java source file test. Java and javac test. Java, the test. Class file is generated in the current directory. Then run Java test to run the program. Of course, you can also specify the location where the class file is generated by using the-D option. For example, javac-d: \ test. Java, You can see test. Class in the D: \ directory.

Section 2: code compilation and running

For writing Java programs, we advocate that both the source file and the class file have the same directory structure, which not only facilitates code organization and management, but also makes fewer mistakes when compiling and running Java programs.

1: Compile and run files in the default package

View code

 //  D: \ SRC \ A. Java  Class  A {  Private   Int  Value;  Public  A () {Value = 0 ;}  Public  Void Setvalue ( Int  VaR) {Value = VaR ;}  Public   Int  Getvalue (){  Return  Value ;}}  //  D: \ SRC \ test. Java  Public   Class  Test {  Public   Void  Static   Void  Main (string [] ARGs) { = New  A (); A. setvalue ( 10 ); System. Out. println (A. getvalue ());}} 

When we run javac test. Java, we will find that there are two files a. Class and test. Class in the D: \ src directory. Run Java test and the output is 10 in DOS. We found that there are no package statements in these two java files, so these two files belong to the same default package (the files that do not show writing package belong to the default package ).

2: Java class discovery rules

To compile a Java file successfully, you must use classpath and package name together.

Class discovery rules: the directory where the class file is located is = classpath + '\' + '.' In the package name is changed '\'

This rule must be remembered to ensure that everything is OK! Java relies on this rule to find the desired class file.

Example: classpath = D:

View code

 //  D: \ SRC \ A. Java  Package  SRC;  Public   Class  A {  Private   Int  Value;  Public  A () {Value = 0 ;}  Public   Void Setvalue (Int  VaR) {Value = VaR ;}  Public   Int  Getvalue (){  Return  Value ;}}  //  D: \ test. Java  Import SRC .* ;  Public   Class  Test {  Public  Void   Static   Void  Main (string [] ARGs) { = New  A (); A. setvalue ( 10 ); System. Out. println (A. getvalue ());}} 

If you want javac test. Java to succeed, you must find a. Class in the D: \ SRC (D: \ src = D: \ + SRC) path. Otherwise, the compilation fails. If the Directory D: \ SRC does not exist, an error message indicating that the package name does not exist is returned. If D: \ SRC exists, but a. class does not exist in this path, the class file is not found in the report. If the directory where the Java file is stored is correct and the package where it is stored is also correctly specified, javac will automatically compile a. Java to generate a. Class.

For example, in the preceding example, all required class files can be correctly generated. However, if the package statement in A. Java is removed. Compile a. Java to generate the. Class file, and then run javac test. Java under D: \. It will not succeed, although the. Class file exists under D: \ SRC.

Therefore, the package statement must have a relationship with the directory where the generated class file is located, so that javac can find the corresponding class file and verify whether a file is in the correct package.

So what is classpath? You can also guess from the name. classpath represents the storage path of the class file. This is an environment variable that can be added to the user's environment variable. Its value is set based on the specific package name during programming and the storage path of the class. If you do not want to use environment variables, you can also use-classpath to set them during compilation.

Classpath can be used to set multiple paths, which are separated. During compilation, javac will try the combination of each path of classpath and the package name of the file one by one until the corresponding class file is found. However, if a class with the same name is found in multiple different paths through the combination of classpath and package name, an error message is returned.

Because javac generates class files in the current directory of the Java file by default, you often add '.' To classpath to represent the path of the current file. Later versions of Java compilation and running programs may automatically add '.' To classpath. In the above example, because test. Java is located in the upper-level directory of A. Java, the classpath can be compiled successfully when it is set.

Set classpath to '.', which indicates the current running directory. So what is the current running directory? It is actually the directory where javac is running. Note: it cannot be considered as the directory of the currently compiled Java file.

If the package packagename is written in the Java file, the package is specified for the class file, and the class file must also be in the corresponding subdirectory after packagename conversion. If this parameter is not specified, the class file is in the default package.

For now, it is like running a Java program.
Example: classpath = D:

View code

/ D: \ SRC \ A. Java Package  SRC;  Public   Class  A {  Private   Int  Value;  Public  A () {Value = 0 ;}  Public   Void Setvalue ( Int  VaR) {Value =VaR ;}  Public   Int  Getvalue (){  Return  Value ;}}  //  D: \ SRC \ test. Java  Package  SRC;  Import SRC .* ;  Public   Class  Test {  Public  Void   Static   Void  Main (string [] ARGs) { = New  A (); A. setvalue ( 10 ); System. Out. println (A. getvalue ());}} 

How can we run this program?

First, run javac test. Java in the src directory.

Then run Java SRC. Test in any directory.

Note: Do not run the Java test command, and the system will prompt that the class error cannot be found (even in the directory where the test. Class file is located ). Therefore, when running a specified package program, you need to display the package name, namely, Java packagename. classname follows the same search rules as the class file, that is, in classpath + packagename '. the class file indicated by the corresponding classname can be found under the path indicated.

Section 3: how to pack a jar package and use a jar package

Open a jar package and use the jar command.

View code

 //  D: \ SRC \ A. Java  Package  SRC;  Public   Class  A {  Private   Int  Value;  Public  A () {Value = 0 ;}  Public   Void Setvalue ( Int  VaR) {Value = VaR ;}  Public   Int  Getvalue (){  Return  Value ;}} 

First, run javac A. Java in the src directory to generate a. Class. Then run jar-CVF test. Jar Src in the D: \ directory to generate test. jar in the Directory D. How can we use this jar package? First, you must add the path test. jar to classpath. Then we have the following test program.

View code

ImportSRC .*;//This import must be available because a. Class is located in the SRC package. If no data is written, the compilation fails.Public ClassB {Public Static VoidMain (string [] ARGs) {=NewA ();}}

The jar file is actually a compressed package, which can be easily decompressed using the decompression software. There is a directory relationship after decompression:

 
Test. Jar | -- SRC --- | -- A. Java | -- A. Class

Test. jar files are processed as directories. javac automatically decompress the test file in classpath. jar and according to B. in Java, find. class file and use it.

Now let's take a look at our packaging command jar-CVF test. Jar SRC // It means to package the entire SRC folder

If we package it like this: Run jar-CVF test. Jar A. Java A. Class in the src directory, OK? The answer is no. Because after packaging, the path relationship is:

 
Test. Jar | -- A. Java | -- A. Class

When javac looks for a. Class, it finds that the src directory does not exist in test. jar, and an error indicating that the package does not exist will be reported. Therefore, you must note that the directory should be put into the package, rather than the directory. If you package a class file that belongs to the default package, you only need to add the jar package path to classpath to use it without using import in the program. Of course, the jar command also has many optional parameters to meet the packaging requirements of different needs.

4: use makefile to automatically compile and run Java programs

The following is a simple example: In this example, the Windows environment variable is not manually set, but is specified with-classpath to facilitate porting between different machines.

 

The following directory structure:

Project --- | --- SRC --- | --- main --- | --- test. java | --- package1 --- | ---. java | --- B. java | --- package2 --- | --- C. java | --- makefile | --- classes --- | --- main --- | --- test. class | --- package1 --- | ---. class | --- B. class | --- package2 --- | --- C. class

The Code is as follows:

View code

 //  A. Java  Package  Package1;  Public   Class A {  Private   Int  Value;  Public A () {value = 0 ;}  Public   Void Setvalue ( Int  VaR) {Value = VaR ;}  Public   Int  Getvalue (){  Return Value ;}}  //  B. Java  Package  Package1;  Public   Class  B {  Private  String name;  Public B () {name = Null  ;}  Public   Void  Setname (string name ){ This . Name = Name ;}  Public  String getname (){  Return  Name ;}}  //  C. Java  Package  Package2;  Public   Class  C {  Private  String name;  Public C () {name = Null  ;}  Public   Void  Setname (string name ){  This . Name = Name ;}  Public  String getname (){  Return  Name ;}}  //  Test. Java  Import Java. util .* ; Import Package1 .* ;  Import Package2 .* ;  Public   Class  Test {  Public   Static   Void  Main (string [] ARGs) { = New  A (); B = New  B (); C =New  C (); A. setvalue ( 3 ); B. setname ( "Java" ); C. setname ( "Test" ); System. out. println (. getvalue (); system. out. println (B. getname (); system. out. println (C. getname ());}} 

Makefile is as follows:

 
// Makefiletarget: javac. /package1/. java-d .. /classes javac. /package1/B. java-d .. /classes javac. /package2/C. java-d .. /classes javac. /main/test. java-d .. /classes/main/-classpath .. /classesrun: Java-classpath .. /classes/main ;.. /classes testclean: Rm-RF .. /classes/package1 /*. class Rm-RF .. /classes/package2 /*. class Rm-RF .. /classes/main /*. class

Run: Make
D: \ Java \ project \ SRC> make
Javac./package1/a. Java-D ../classes
Javac./package1/B. Java-D ../classes
Javac./package2/C. Java-D ../classes
Javac./main/test. Java-D ../classes/main/-classpath ../classes

Run: Make run
D: \ Java \ project \ SRC> make run
Java-classpath ../classes/main; ../classes Test
3
Java
Test

In this way, we can compile and run our own system without using eclipse, netbeans, and other development tools.

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.