First, the problem of classpath
CMD is compiled to run Java, using e:/tmp as the path file name can be different from the class name, but if it is a class defined by public, it must be the same as the file name, but is usually consistent. Case 1 single class without package path source code Test.javapublic class Test {public static void main (string[] args) {System.out.println ("Hello");}} Compile Javac Test.java run Java Test Case 2 single class with package path source code Hello.javapackage Zhangsan.lisi;public class Hello {public static void main (S Tring[] args) {System.out.println ("Hello");}} Method One: Compile the Javac Hello.java build package level after the compilation is complete, place the. class file under the Zhangsan/lisi folder method two: Use javac-d. Hello.java will be generated according to the package path in Hello.java. You can also compile multiple files javac-d at the same time. A.java B.java run in Zhangsan Java Zhangsan.lisi.Hello at this time set Classpath:set classpath=e:/tmp, after setting classpath, at any other drive letter location, Executable Java Zhangsan.lisi.Hello. Conditions 32 class reference cases, two classes in the same location main class in E:\test\com\ny\Hello2.class another class E:\test\com\ny1\Hello3.class source code hello2.javapackage com.ny; Import Com.ny1.hello3;public class Hello2 {public static void main (string[] args) {new Hello3 (). Say ();}} Source code Hello3.javapackage Com.ny1;public class Hello3 {public void Say () {System.out.println (' nice ');}} Description: Hello2.java references the function of Hello3.java, to compile the Hello3 first, and then the Hello2 can be referenced. Compilation can also be javac-d. Hello2.java Hello3.java Case 42 class references another class in another location main class in E:\test\com\ny\Hello2.class another class E:\com\ny1\ Hello3.class Configuring content in environment variable Classpath ".; E:\ ",". " Mark the current path, "E:\" indicates that the path where the Hello3 is located can be modified by means of a command: Set classpath= (set to NULL) Two: Set classpath =.; E:\;classpath Description: The compiler is now looking under the current path, if the class is not found to the CLASSPATH environment variable path to find, can not find an error.
Ii. path issues for Java projects in eclipse
Absolute path: c:/a.txt,/var/xx/aa.txt relative path: For example, in the C: disk, A.txt's full path is c:/a.txt, the relative path is a.txt. Represents the current folder,. Indicates a parent folder example: View path, not practical eclipse tool, use command line to compile execution, path to E:\tmppackage Zhangsan.lisi;import Java.io.file;public class Pathtest { public static void Main (string[] args) throws Exception {System.out.println (New File ("A.txt"). GetAbsolutePath ());}} Compile: javac-d. Pathtest.java execution: Java zhangsan.lisi.PathTest result: E:\tmp\a.txt will e:\tmp join Classpath,set classpath=e:\tmp, execute Java under f: Disk Zhangsan.lisi.PathTest results for f:\a.txt based on the above analysis, you can see that the default path for A.txt is the path where the Java command is executed. In the Eclipse project, establish the Pathtest class for path testing, where the path to E:\workspacemy\test\a.txt,a.txt is the Workspace\ project name, and the starting point is the path that the Java command executes.
In the Eclipse Project, the compiled project is added to Classpath, which means that you can get the resource file in Classpath. Example: Get the properties file under Classpath package Test;import Java.io.inputstream;import Java.util.properties;public class Test2 {public static void Main (string[] args) throws exception{properties Properties = new properties (); Class Clazz = Test2.class;//inputstream in = Clazz.getresourceasstream ("my.properties"); From Classpath down find, know find location, because the position is not ok inputstream in = Clazz.getresourceasstream ("/my.properties"); /Represents the root path of the classpath, My.properties is the file under the root path, that is, the bin location properties.load (in); In.close (); System.out.println (properties);}} Results can be unique my.properties properties file
Every MyEclipse project has a. Classpath with the. project file. Classpath describes the information that references the jar and adds the corresponding class to classpath. Project description information about the project
Iii. path issues for javaweb projects in eclipse
/use of the distinction to the server used:/means the root directory of the current Web application (Webroot) for use by the browser:/means in the root directory of the WebApps 1. Forwarding Request.getrequestdispatcher ("/target.html" ). Forward (request, response); 2. Requests redirect Response.sendredirect ("/day11/target.html"); 3. HTML page Hyperlink Hrefresponse.getwriter (). Write ("
Java Path Issues