The Java command line executes those things __java

Source: Internet
Author: User

When you first started learning Java, it was a good idea to execute Java programs from the DOS command line, which is only for Windows environments. Now the general summary of their own after the attempt to encounter some problems, and solutions.

First, look at the command line to see if the Java program is installed, and print the classpath and Java_home variables:


Through these three, you will be able to know your environment configuration is OK
If the implementation of java-version error or can not find, it should be not configured Java PATH environment, please own Baidu configuration.

Java-related Terminology description
Here is only a description of today's theme: Classpath, as for java_home and path please own Baidu.

Set Classpath:
This variable setting is important, and it's about telling the JVM to go to the directory to find the XXX to execute when the Java Xxx.class is executed. The following are mainly about this to illustrate some of the issues.

Now, start writing a Java simple program:

1. On the desktop right key resume a Hello.java

/**
*author: Nine yi Yang
* *
//package com.godsdk;

public class hello{public

     static void Main (String[]args) {
          //system.out.println ("Hello World:hello");
     }
}

Class _a{public

     static void Main (String[]args) {
          System.out.println ("Hello world:_a");
     }

Class 1test{public

     static void Main (String[]args) {
          System.out.println ("Hello world:1test");
     }

class hello{public

     static void Main (String[]args) {
          System.out.println ("Hello World:hello");
     }

We see that we wrote a few classes in this file named Hello.java, Hello, _a, 1Test, hello;

Now we're going to compile the file:

The error, I believe all clear this problem, this is the main description of the name of the next class naming problems, as for the specific do not want to elaborate, but to emphasize that the following questions, we put 1Test to kill, and then in the compilation:

Strange, how did not claim hello.class, first regardless, executes a first.

The phenomenon is very strange, here are two questions:
1. The desktop obviously has a compiled hello.class, but when the Java hello in the implementation of the error.
2. Execute Java Hello, it can be executed, and output in the Hello class;

Now let's print out the details and see:

!!!!! Newspaper ClassNotFound

That means no hello, but it finds hello. This means that Hello is overwritten with Hello, so the Java class name is not case-sensitive when compiled.

Some of the rules of Java
1. If you define a class that is declared public in the source file, you need to name the name of the source file that contains the class as the class name
2. In the same source file, there can be only one class that is defined as public
3, compile-time filename is not sensitive to the case, the execution of the loaded class name is case sensitive

But at the time of execution, case grateful.

So, the hello at the end of the above is covered with Hello and the error is given when executing Java hello at execution time.

Next, to say another question, I wrote a CD Desktop in the execution.

What is the purpose of this sentence? From the top, you see my classpath environment is ".", which is represented as the current directory

And the first of my directories are:
Dir:c:\users\administrator
In other words, my classpath= "C:\Users\Administrator"

After execution my directory is:
Dir:c:\users\administrator\desttop
Classpath= "C:\Users\Administrator\Desttop" is a bit of a concern, because my. class file is in C:\Users\Administrator\Desttop this directory, so if I'm in In the initial DOS environment to execute Java Hello, the JVM defaults to C:\Users\Administrator Road to find Hello.class, so the implementation of the error.

This one, you can try it manually.

Of course, if someone had to execute the Hello.class in the C:\Users\Administrator\Desttop directory at C:\Users\Administrator,
Can do this

In the above I put desktop Ctrip desttop, please self change.

The execution of CMD from here can be seen using
JAVA-CP C:\Users\Administrator\Desktop Hello World:hello can be executed, where-CP is to specify the CLASSPATH environment variable, that is, the Classpath directory.

2. Compile with Package

First, remove the comments from the package line in the Hello.java file above, and then execute

You will find that you want to generate the class file by the package name directory.

Expand a bit, in fact, in the Windows environment package name can also be capitalized, but it is best not to try in the formal project.

This is because of changes that are made to accommodate different operating systems. Because the operating system of the UNIX system is case sensitive, the "com" and "com" two folders in the operating system are considered to be two different folders, while in the Windows operating system, capitalization is insensitive, so that "com" and "com" Two folders are considered to be the same folder. That's why if someone writes two packages under the UNIX system.

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.