How to compile a Java program with JDK?

Source: Internet
Author: User

Java Hello World Overview
Hou photosensitive

The BBs on many technical websites is filled with help information from beginners of Java about how to run the first Java program. I think it is necessary to write this article as a "coming person, because they all encounter some very low-level problems, but it is too difficult to make these problems occupy so much valuable space on the BBS.
Basic concepts to be clarified
Java is a cross-platform network language. To be able to run on different platforms, it uses the concept of virtual machines. A file suffixed with Java is a source code file, which is compiled to generate a class file. Virtual machines execute these class files and convert them into commands that can be run on different platforms. This is why Java is less efficient. For this reason, there is no concept of EXE files familiar to users in other languages in Java.
How to execute Hello World
Remember: Enter the javac source file name in the command line. Java (Compilation)
Java source file name (run without suffix)
Here we are talking about Java application, which includes the main function.
If Hello world is an applet, enter the name of the appletviewer source file (the compiled class file) in the command line)
If the class file has been embedded in an HTML webpage, you can simply open it with IE.

Application errors

The system prompts exception in thread "Main" Java. Lang. noclassdeffounderror. The cause of this prompt may be:
1. You spelled the file name incorrectly. Java is case sensitive. Of course, the file cannot be found.
2. The required class library is not found on the VM. You may not have set classpath In the autoexec. BAT file. Enter the run of windows, enter sysedit, and then in autoexec. add this line to the BAT file: Set classpath = Your JDK installation directory/LIB; for example, set classpath = C:/jdk1.3.0 _ 02/LIB; Save and exit, execute autoexec again in the C root directory of the DOS window. the bat file has been set. Of course, you can restart the machine if you want.

The system prompts helloworld. Java: 1: Public class helloworld must be defined in a file called.
"Helloworld. Java ".
Public class helloworld {
^
Multiple classes can be defined in a Java source program. However, only one class with the public attribute can be defined and must be consistent with the file name. Also, the main method must be put in this public
To run this class.

The system prompts exception in thread "Main" Java. Lang. nosuchmethoderror: Main. This exception occurs because you copied the code in the book. The main function must be written as follows: public static void main (string ARGs []) cannot be entered incorrectly. Remember !!!

Applet errors

IE shows a gray area, but no hello World is displayed on it. The status bar displays load: calss. Your class file name is no found. This problem occurs because IE does not find the applet (a bit nonsense). You should put the class file in the same directory as the HTML file. If the class file is in the subdirectory of this folder, write code = "subdirectory name. File Name. Class ".
IE indicates that the applet has been started, but only the gray one is displayed. This problem is most likely to occur when JBuilder and other integrated development tools are used, so there is no problem with appletviewer. This is because you use Java versions not supported by IE in the applet. JBuilder uses the swing component by default for applet creation, but ie only supports jdk1.1, which causes this situation. You just need to remove the statements for importing swing, and then follow the prompts to modify the code. Do not use the swing component method. If you think that the swing component is very beautiful and you need to use it all, you can install a Java Plug-in item automatically when you install a high version of JDK. Check if there is any small red-nose item in your control panel. But remember, if you want to use it, your HTML file will be written differently from the previous one. You cannot simply use code to specify the class. As for how to write it, it is not in the scope of this article (you should use plug-in to make it big when writing a hello World ). There are many articles on the Internet about how to write.

Click to execute
When you are familiar with clicking and executing the EXE file, you are naturally not used to opening the DOS window and entering the command line. You can make it into a jar file, but it is a little troublesome for Hello world, there is a simple silly way: Make a batch file by yourself. For example, you can write a run.txt file in a notebook with the following content: Java: Your main file name, and then change the suffix to BAT to save it to the same directory as the main file. It's silly to double-click it.
If you have to run the program using an EXE file, you can use tools such as jet to make the class file an EXE file. However, Java's cross-platform nature is also lost.

The above is the full introduction to Hello world. If you still cannot see "Hello World", I will! You should stop learning Java.

 
  • {
    Try {estimate (this, 'HTTP: // forum.csdn.net/pointforum/forum/estimatereply.aspx? Forumid = Hangzhou & topicid = 00000000-0000-0000-0000-000000883961-14% & postdate = 2002-07-18 + 5738727 3a45% 3a34 & replyid = & type = 0 & V = 13 '); logclickcount (this, 113);} catch (Ex) {} return false;
    } "Href =" javascript:; "> useful to me [0]
  • {
    Try {estimate (this, 'HTTP: // forum.csdn.net/pointforum/forum/estimatereply.aspx? Forumid = Hangzhou & topicid = 00000000-0000-0000-0000-000000883961-14% & postdate = 2002-07-18 + 5738727 3a45% 3a34 & replyid = & type = 1 & V = 13 '); logclickcount (this, 114);} catch (Ex) {} return false;
    } "Href =" javascript:; "> throwing bricks [0]
  • {
    Try {quote (2) ;}catch (Ex) {}; return false
    } "Href =" javascript:; "> reference
  • {
    Try {Report (2);} catch (Ex) {} return false;
    } "Href =" javascript:; "> report
  • {
    Try {showmenu (this, 'HTTP: // forum.csdn.net/pointforum/manage/modifyreply.aspx? Forumid = 3931c03e-b66c-4189-a4f0-210fd9501803 & topicid = 00000000-0000-0000-0000-000000883961 & replyid = 5738727 & postdate = + 14% 3a45% 3a34 & V = 13 ', 'HTTP: // forum.csdn.net/pointforum/manage/deletereply.aspx? Forumid = 3931c03e-b66c-4189-a4f0-210fd9501803 & topicid = 00000000-0000-0000-0000-000000883961 & replyid = 5738727 & postdate = 2002-07-18 + 14% 3a45% 3a34 & V = 13 '); return false} catch (Ex ){}
    } "Href =" javascript:; "> Manage
  • Top
Excellent recommendations :{
Logclickcount (this, 112 );
} "Href =" http://topic.csdn.net/u/20080830/23/6ab34315-49d0-46d6-9674-87ad23db5db0.html "target =" _ blank "> the problem is choice.


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.