About classpath paths in Java projects

Source: Internet
Author: User

Self-Reading summary: The configuration of "classpath:" equivalent to the output directory, that is, the compiled class file directory, and "file:" is the startup directory, 14 on the server is the Web-inf directory, the local project is the project root directory. 

About classpath paths in Java projects in detail: Java 2013-03-14 10:52 6845 people read reviews (1) Favorites report compiling Java programs under DOS requires the concept of classpath, especially when no environment variables are set. Classpath is the path to a compiled file that stores. class. Javac: If you are currently compiling a Java file that references other classes (such as inheritance), but the reference class's. class file is not in the current directory, you need to add the-classpath parameter after the Javac command, by using the following three types of methods To instruct the compiler to look for reference classes under the specified path at compile time. (1). Absolute path: Javac-classpath C:/junit3.8.1/junit.jar Xxx.java (2). Relative path: Javac-classpath. /junit3.8.1/junit.javr Xxx.java (3). System variables: Javac-classpath%classpath% Xxx.java (Note:%classpath% Represents a lookup using the value of the system variable classpath, where the Junit.jar path is assumed to be included in the CLASSPATH system variable) javac Absolute path Usage: Javac: Assume that the class file you are compiling is named: Helloworld.java, whose full path is: D:/java/helloworld.java. But your current directory is: C:/Documents and settings/peng>. What happens if you want to perform the compilation here? (1). C:/Documents and settings/peng> Javac helloworld.java The compiler will give the following error message: Error:cannot read: Helloworld.java this is because by default Javac is looking for the class file in the current directory, it is obvious that this path is not where we store the class file, so it will be an error (2). C:/Documents and Settings/peng>javac D:/java/helloworld.java compile successfully. So, as long as the directory where you execute the JAVAC command is not the directory where the class file resides, you must explicitly specify the path to the class file in the Javac command. Use of Java-classpath: Java: Suppose our CLASspath is set to: D:/peng/java/pro, there are three files in this directory: Helloworld.java/helloworldextendstestcase/helloworldextendshelloworld. The class declarations for these three files are as follows: HelloWorld.java:public class HelloWorld HelloWorldExtendsHelloWorld.java:public class Helloworldextendshelloworld extends HelloWorldHelloWorldExtendsTestCase.java:public class Helloworldextendstestcase extends Junit.framework.TestCase assumes that we have successfully compiled three files according to the use of the absolute path of Javac-classpath and Javac above 。 Now we execute these three. class files (1) under the C:/Documents and settings/peng> directory. C:/Documents and Settings/peng>java HelloWorld Hello World can see the execution succeed. Why do we execute commands in C:/Documents and settings/peng> that the JVM can find d:/peng/java/pro/helloworld.class files? This is because we configured the system variable Classpath and pointed to the directory: D:/peng/java/pro. So the JVM will default to the directory to load the class file, without having to specify the absolute path to the. class file. (2). C:/Documents and Settings/peng>java Helloworldextendshelloworld Hello World can see the execution succeed. Helloworldextendshelloworld inherits the HelloWorld class, So at execution time the JVM will find out if there is a Helloworld.class file under Classpath because we have successfully compiled the HelloWorld class, so we can execute it successfully helloworldextendshelloworld. Class (3). C:/Documents and Settings/peng>java helloworldextendstestcase Exception in thread "main" Java.lang.NoClassDefFoundE Rror:junit/framework/testcase can see that the program throws an exception, prompting to find the Junit.framework.TestCase file. Why is the same under:/peng/java/pro, Helloworldextendshelloworld.class can be executed successfully, and this is not? This is because the Junit.framework.TestCase.class file does not exist in the current directory, so in order for the program to run successfully, we have to specify the Classpath way to allow the JVM to find the Junit.framework.Tes Tcase This class, such as (4): (4). C:/Documents and Settings/peng>java-classpath%classpath% helloworldextendstestcase Hello World Summary: (1). When to use-clas Spath: When you are compiling or executing a class that references other classes, but the. class file of the referenced class is not in the current directory, you need to introduce the class (2) through-classpath. When to specify a path: When you are compiling a directory that contains a class that is not the same directory as the directory in which you are executing the JAVAC command, you need to specify the path to the source file (Classpath is used to specify the path of the. class path, not for the. java file) Java gets classpath path: ClassLoader provides two methods for obtaining resources from the loaded classpath: public URL getresource (String name); Public InputStream getResourceAsStream (String name); Here name is the classpath of the resource, which is relative to the location under the "/" root path. GetResource gets a URL object to locate the resource, and getResourceAsStream obtains the reference to the resource's input stream to ensure that the program can extract the data from the correct location. ButThe real use of these two methods is not ClassLoader, but the GetResource and getResourceAsStream methods of class, because class objects can be obtained from your class (such as Yourclass.class or Yourclass.getclass ()), and ClassLoader needs to call the Yourclass.getclassloader () method again, but according to the JDK document, these two methods of the class object are actually "delegates" (delegate) for the ClassLoader to load it, so only the two methods of using the class object are required. Therefore, the direct call to This.getclass (). getResourceAsStream (String name); Gets the stream, and the static method uses the Classloader.getsystemresourceasstream ( String name);. Here are some ways to get the absolute path of classpath and the current class. You may need to use some of these methods to get the absolute path of the resources you need. 1.this.getclass (). GetResource ("") gets the URI directory of the current class-classes file. Don't include yourself! such as: File:/d:/workspace/jbpmtest3/bin/com/test/2.this.getclass (). GetResource ("/") gets the absolute URI path of the current classpath. such as: File:/d:/workspace/jbpmtest3/bin/3.this.getclass (). getClassLoader (). GetResource ("") gets the absolute URI path of the current classpath as well. For example, File:/d:/workspace/jbpmtest3/bin/4.classloader.getsystemresource ("") obtains the absolute URI path of the current classpath. such as: File:/d:/workspace/jbpmtest3/bin/5.thread.currentthread (). Getcontextclassloader (). GetResource ("") The resulting absolute URI path is also the current classpath. such as: File:/d:/workspace/jbpmtest3/biN/6.servletactioncontext.getservletcontext (). Getrealpath ("/") in the Web application, get the absolute path to the root directory of the Web application. This way, we only need to provide a path relative to the root of the Web application to build the absolute path to the location resource. such as: file:/d:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ WebProject Note: 1. Try not to use relative paths to the current user directory compared to System.getproperty ("User.dir"). This is a time bomb that may kill you at any moment. 2. Use absolute path resources in the form of URIs as much as possible. It can easily be transformed into a Uri,url,file object. 3. Try to use relative classpath relative paths. Do not use absolute paths. Using the public static URL Getextendresource (String relativepath) method of the Classloaderutil class above, you have been able to locate resources for all locations using relative paths to classpath. 4. Never use a hard-coded absolute path. Because, we can use the ClassLoader class's GetResource ("") method to get the absolute path of the current classpath. If you must specify an absolute path, then using a configuration file is much better than hard coding! Method for obtaining a path other than classpath: URL base = This.getclass (). GetResource ("");//Obtain the position of this class first, such as/home/popeye/testjava/build/classes/ net/string path = new File (Base.getfile (), ".../....../....../" +name). Getcanonicalpath ();//You can get/home/popeye/testjava/name In addition, if you start the program from Ant, This.getclass (). GetResource ("") take out the more strange, directly with the Java command Line debugging can be successful. PS: Many of the above content from reprint and own part of the summary.

About classpath paths in Java projects

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.