0

I find a project in Eclipse has it own class path.

If i use maven create a new web project.

System.getProperty("java.class.path"); 

When i print the result, I find the class path contains G:\newtool\workspace\springmvc\target\classes.

Who can explain the principles behind it.Why the class path is this directory. Is the build function of Eclipse same as the command,javac -classpath?

And i find another question.

public class KnightMain { public static void main(String[] args) throws Exception { System.setProperty("java.class.path","G:/newtool/workspace;G:/newtool"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("knight.xml"); Knight knight = context.getBean(Knight.class); knight.embarkOnQuest(); String s[] = System.getProperty("java.class.path").split(";"); for (String string : s) { System.out.println(string); } context.close(); } 

}

Although i set the class path to other directory where the knight.xml is not exist.But ClassPathXmlApplicationContext find it finally.Why the

System.setProperty("java.class.path","G:/newtool/workspace;G:/newtool"); 

makes no difference.Although the print result is:

G:/newtool/workspace G:/newtool 
3
  • Where is your context definition file located? Usually the root class location is considered as part of the classpath implicitly. Commented Sep 20, 2016 at 3:53
  • The xml file is in the directory,src/main/java. Commented Sep 20, 2016 at 3:59
  • If this is where your main class located, then it is in the classpath implicitly. Commented Sep 20, 2016 at 4:04

2 Answers 2

4

Two different tools, two different concepts.

First of all eclipse has its own java compiler. It is not using javac in any way. And the class path used by the eclipse compiler, is, surprise, what you told it to be.

In other words: when you create your eclipse projects, you configure its build path. The Build Path determines which libraries, other projects, ... should be imported, exported, ... and so on.

Whereas, when you open your command like shell, and you call javac directly, the classpath is based on the settings that you make there; for example by calling

javac -classpath /some/directory/with/classes:/some/bla.jar 

Or in your case: you are using maven, which comes with its pre-defined rules, project dependencies, and so on. Thus your classpath there depends on the conventions/rules that maven applies for "java web projects". Thus: if you want to understand what maven is doing for you: you have to study the maven documentation for the features you are using there!

EDIT: I think what you are actually trying can't work. I think that this specific system property is more of a "read only" value. Meaning: you can use it to understand the current classpath. But writing the property will not change the classpath of the JVM you are currently running in. Actually, that makes a lot of sense: if any piece of java code could change the classpath on the fly, that screams "security problem" all over the place! As that would allow you to completely change where the JVM would be loading its classes from.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks!Could you help me to look at the question after i edit.Because i find another question.
Sorry; I am not an expert on maven/spring itself; I fear I can't help with that specific part.
You can ignore Spring.The ClassPathXmlApplicationContext class will find the xml in class path.I am confused that why the class path did not change.
See my updated answer then. I think what you try to do ... simply is not possible.
In general, what GhostCat is saying is correct: The default class loader is initialized when JVM is constructed and reads the java.class.path just once during initialization. If you change the property later, it doesn't affect the default loader. If you create another loader after modifying the property, then it depends on the loader implementation. Now, I am not an expert on Spring, but I assume similar rules can apply. If you can reload the entire spring framework after changing properties, this might work.
|
1

The java.class.path property is used by JVM to locate classes and JAR files for loading. This system property is common for most available JVMs including Oracle and IBM implementations. This property is used by the default class loader, but not all custom class loaders. You can implement your own class loader, that ignores java.class.path and is using some other property to locate loader-specific packages. One example is Eclipse. Eclipse core has a custom class loader that is not using java.class.path.

3 Comments

So,the class path of Eclipse is based on the project you build? Like i use maven build the project,the class path set as G:\newtool\workspace\springmvc\target\classes by default?
Different rules apply to the Eclipse itself and applications started by Eclipse. For applications started by Eclipse, properties like classpath can be defined in the project. For Eclipse itself rules are different. I do not think you are interested in the Eclipse class loader configuration. It is mostly used for loading Eclipse Core classes.
Yes,it is in the root class location.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.