Error in compiling file on Java command line, no symbol or package not found __java

Source: Internet
Author: User
error occurred while compiling file on Java command line, no symbol or package not found


tags (space-delimited): Java



Reference Http://stackoverflow.com/questions/6665420/package-does-not-exist-error
Reference http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html



matching code uploaded in : http://download.csdn.net/detail/qq369201191/9287185



Accustomed to the automatic compilation of Eclipse, the most basic part of the Java command-line compilation and execution of a file is the compilation and execution of a single file and does not contain any external jar packages. But sometimes you have to use the command line, you will encounter some problems, bloggers here are several common problems and solutions. 


Introduction of experimental environment




Eclipse's working directory in D:\myeclipse\Workspaces, which has one of our engineering example, points in, and Eclipse has set up several files for us. Settings, bin, SRC,. Classpath, The source code is stored in the PROJECT,SRC, and the structure is shown in the image above.



The structure shown after Eclipse import is shown in the following illustration,

Tip: Package name Com.example.controller refers to having a Controller.java file under the Src/com/example/controller folder



Model.java is a separate class that does not import any external packages
Controller.java imported The Com.example.model.model
View.java imported Com.example.controller.controller and called an external jar package



When you look at the case code, you need to pay special attention to the current directory , option Parameters , and Java files for the command line at this time, with different results using an absolute path



Absolute path is the path from the root directory, Windows is the beginning of the letter, such as "C:\", Linux under the beginning of a slash/home.



The advantage of using an absolute path is that no matter where the current path of the command line is, it compiles correctly



Case A



D:\myeclipse\workspaces\example>javac D:\myeclipse\workspaces\example\src\com\example\model\*.java


Case Two



D:\myeclipse\workspaces\example\src>javac D:\myeclipse\workspaces\example\src\com\example\model\*.java
Using relative PathsA relative path is relative to the current context, and in the command line is the previous path in the relative command line. Relative path does not start with/, direct file name.

As in case one, the current path is D:\MYECLIPSE\WORKSPACES\EXAMPLE\SRC, relative to this path, the relative path of the Java file is Com/example/model/*.java. The current path changes in case two, and the relative path of the corresponding Java file changes.



Case A



D:\myeclipse\workspaces\example\src>javac Com/example/model/*.java


Case Two



D:\myeclipse\workspaces\example>javac Src/com/example/model/*.java


The basic concept has been understood, on dry goods, General Command line compilation problems: problem one: no symbol found or package does not exist



Case A



D:\myeclipse\workspaces\example\src>javac Com/example/controller/*.java


In this case it is normal to compile, because you do not specify CLASSPATH, the general system default classpath will search in the current path.



Case Two



D:\myeclipse\workspaces\example>javac Src/com/example/controller/*.java

Error
src\com\example\ Controller\controller.java:3: Package Com.example.model does not exist


We switch the directory, the absolute path above, the relative path knowledge, and correctly give the path to the Java file. An error is reported and the package com.xxx does not exist. Because there is an import Com.example.model.model in the Controller.java file, depending on the system default classpath will be found under the current path, that is, D:\myeclipse\Workspaces\ example, obviously there is no COM file under this path.



Workaround, we tell the compiler which directory will have the line, so we need to add options-cp xxxx or-classpath xxxx



D:\MYECLIPSE\WORKSPACES\EXAMPLE>JAVAC-CP src Src/com/example/controller/*.java


Plus the parameters-cp src, tell the compiler can go to src find, there will be no problem.



Similarly, classpath can be a relative path or an absolute path, in this case the relative path is used, and after the command line is changed, all parameters are changed



D:\MYECLIPSE\WORKSPACES>JAVAC-CP example/src Example/src/com/example/controller/*.java


The following is written with an absolute path, the relative path and the absolute path are no longer described



JAVAC-CP D:\myeclipse\Workspaces\example\src Example/src/com/example/controller/*.java
question two: How to introduce jar packs


First or in eclipse, it is recommended to create a Lib file, and then put all the jar package here, the project transfer is also more convenient.



Case A



D:\myeclipse\workspaces\example\src>javac Com/example/view/view.java

Error
Com\example\view\view.java:4 : Package Com.google.gson does not exist
import Com.google.gson.Gson;
                      ^
com\example\view\view.java:14: symbol symbol not found
: class Gson
Position: Class Com.example.view.view
                Gson Gson = new Gson (); c8/>^
com\example\view\view.java:14: symbol symbol not found:
class Gson
Position: Class Com.example.view.view
                Gson Gson = new Gson ();
                                ^
3 Error


In fact, a problem, did not find the jar package. The introduction of a jar is to note that the path is written to the location of the jar package. Less nonsense, give examples:



Case Two



D:\MYECLIPSE\WORKSPACES\EXAMPLE\SRC>JAVAC-CP Lib/gson-2.3.1.jar Com/example/view/view.java

error
com\ Example\view\view.java:3: Package Com.example.controller does not exist
import Com.example.controller.controller;
                             ^
com\example\view\view.java:10: symbol symbol not found
: Class controller
Position: Class Com.example.view.view
                new Controller () ;
                    ^
2 Error


Careful you will find that the problem has changed, because we introduced the jar package, so because the jar can not find the Gson problem has gone, the problem is now is not found com.xxxx, become a problem. Here to explain, we join-CP xxxx or-classpath xxxx, will overwrite the classpath in the system, case two so write can find Gson class, but when encountered import Com.example.model.model still in Lib /gson-2.3.1.jar file in search Of course is not found, so the error. The method is simple, in addition to the path of a com.xxxx folder, yes is the current path. So Add.; Lib/gson-2.3.1.jar. Now if you are configuring the Java environment in the first place, the path in the classpath should be.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar has understood it.



The special note is to use a colon in Linux: separate, while Windows splits with semicolons.



Case Three



Correct
D:\MYECLIPSE\WORKSPACES\EXAMPLE\SRC>JAVAC-CP.; Lib/gson-2.3.1.jar Com/example/view/view.java
question three: Specify a location for class file storage


By default, class files and Java files are stored in a directory, but you have not found that eclipse after the compilation will have two directories, one is SRC, the store is the user edit the source code, set a bin file, is compiled byte code. It is the-D option added.



D:\myeclipse\workspaces\example\src>javac-d.. /bin Com/example/model/model.java


Here bin file and src file is the same sibling (see Experimental environment Introduction), is also used is the relative path. When you open a file, you can see that a Com/example/model/model.class hierarchy is generated in the bin file.



I believe that you have been able to basically solve the compile-time problem. 


Summary



This article gives a few solutions to common problems, the above code has been tested, there will be no problem. On the command line we want to pay attention to the difference between the relative path and the absolute path, the difference between the Linux and Windows delimiters (Linux with colons, windows with semicolons). When you set up classpath, you usually add the current path.



The above content is the blogger to consult some data, and carried on the summary, originally also wanted to study the official document carefully, but because is all English, and very concrete, still gave up. Later in the face of problems continue to improve the good. If there are any problems mentioned above, please point out in time, welcome to exchange.


Related Article

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.