Java for the first time

Source: Internet
Author: User
Last night, I successfully ran my first Java program. Very excited, for the first time. Excitement is still a little to remind yourself or as I just started Java learning friends.


actually last night I had successfully compiled HelloWorld (the most classic program, all of my programming language learning from her), but at the time of running the following error occurred:


Exception in thread "main" Java.lang.nosuchmethoderror:helloword/class


what reason? I search for solutions on the Internet found the following article:







There are so many people
ask HelloWorld questions, and they often start with "Ask one of the simplest questions". Its


actually recalls, oneself also from this stage come over, say a "hello", really is a simplest question


? ...//think well, let's say "helloworld!" in Java ...


first of all, let's assume that our platform is windows+jdk (similar to the Linux environment). This environment


is quite common, basic and introductory. Make sure that the JDK is installed properly, and the next step is to carefully tap a certain teaching


HelloWorld source code, save, and then compile, javac ... Here's the problem:


* ERROR 1:


' Javac ' is not an internal or external command, nor a running program or batch file.


(Javac:command not found)


The reason for this is that the environment variable path is not set well. Win98 under the Autoexce.bat add path=%path%;c:\jdk1.2\bin,win2000 under the Control Panel-> system-> advanced-> Environment Variables-> System variables ... See? Double-click Path and add C:\jdk1.2\bin to the back. Of course, we assume that JDK is installed in the c:\jdk1.2 directory (a little monk?) )... It seems like we have to reboot the system to work ... (//know!) tomatoes) OK, try again! Javac HelloWorld





* ERROR 2:


HelloWorld is a invalid option or argument.


please, give a little professionalism, Java source program is sure to save into a. java file, and compile-time to write full. java.


OK, Javac Helloworld.java (This is the time to become it? )





* ERROR 3:


helloworld.java:1: public class HelloWorld must is defined in a file called


"Helloworld.java".


public class helloworld{


^


This problem is because your class name is inconsistent with the name of the file. (who said, obviously see people have this written () OK, to be exact, a Java source program can define multiple classes, however, a class with public properties can only have one, and to be consistent with the file name. Also, the main method must be placed in this public class, so that it can be Java (run) this class. Another point is that the Java language is strictly case-sensitive, beginners should pay attention to it. As in the example above HelloWorld and HelloWorld are considered different, so ... oh ... OK, change it, hee ... javac helloworld.java ... Why is there nothing? //faint This is the compilation passed! See if there's one more helloworld.class? (hehe..) According to the book taught: Java HelloWorld (!! This I know, not Java Helloworld.class yo)





* ERROR 4:


Exception in thread "main" Java.lang.NoClassDefFoundError:HelloWorld Oh, well, this is the famous class path (classpath) problem. In fact, Classpath is a concept in Java that is involved in the compilation process. Classpath is to indicate where to find the use of the class, it is so simple. Since our HelloWorld does not work with other classes (not Java.lang packages), this problem is not encountered at compile time. When running, you should indicate where your class is. Workaround, you can run it with the following command:


Java-classpath. HelloWorld "." Represents the current directory. Of course it's a bit of a hassle ("Too Much Trouble"). , we can set the default classpath in the environment variable. Method is as set as the path above. Set the classpath to:


classpath=.; The two recommendations behind the C:\jdk1.2\lib\dt.jar;c:\jdk1.2\lib\tools.jar are also set up


, later developed with the. Java-classpath. HelloWorld (I will not learn Java)





* ERROR 5:


Exception in thread "main" Java.lang.NoSuchMethodError:main


(//BAM) don't, hang on. Look at your code, the question is in the definition of the main method, is it in the right place,

Is that what
wrote:


public static void Main (String args[]) {//One word is not bad, don't ask why first ...


yes, including case!


Java-classpath. HelloWorld (resigned!) )


Hello world!


(faint! finally ...) )


Welcome to the Java World! So, the inability to run HelloWorld is really not a "simplest question".


attached: Helloworld.java


//Helloworld.java


public class HelloWorld {


public static void Main (String args[]) {


System.out.println ("Hello world!");








}





Haha found the crux of the error 5, but the author mentioned the error I have no fault, how to do? Look at their own program for a long time, finally I found that I am running Java HelloWorld, in order to save trouble, I entered the "Java Hel ...", in order to save trouble, directly press the TAB key, can be imagined that the command became like this: "Java helloworld.class" So there's the error in the hint, Because Java cannot have a suffix when it runs a compiled. class file. Class (This is what I later know, didn't know at the time), this thing let me understand: the more the most details of the problem is easier to be negligent, and such negligence is the most difficult to find, so write procedures must put the heart in the most easily overlooked place. Write down this article to remind yourself and friends like me that there will be no such negligence in the future. (This may be fatal to programmers)








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.