# maven project file path

Hi,

I am trying below example

http://technojeeves.com/joomla/index.php/free/132-find-the-current-directory-in-java

when I right click on the CurrentDirectory java file (which is under maven structure 'src/main/java' nd then package 'com\aa\bb\cc\dd\ee\') and click properties it shows

C:\GPDesktop\workspaces\workspace_final\my_ui\maven_app\maven_app-web-app\src\main\java\com\aa\bb\cc\dd\ee\CurrentDirectory.java

package aa\bb\cc\dd\ee
public class CurrentDirectory {
public static void main(String[] args) {
System.out.printf("Current directory is %s\n", System.getProperty("user.dir"));
}
}


But when i run CurrentDirectory java file as 'java application'

I see console output as

C:\GPDesktop\workspaces\workspace_final\my_ui\maven_app\maven_app-web-app

I have not understood why current directory shows only "C:\GPDesktop\workspaces\workspace_final\my_ui\maven_app\maven_app-web-app
" and filters, removes "\src\main\java\com\aa\bb\cc\dd\ee\"

I wonder why the package names did not came on the console. What is the purpose of packages if they do not show in the current directory.

how is the classpath related to this.

LVL 7
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IT Business Systems Analyst / Software DeveloperCommented:
The "current directory" (also know as the "working directory") has absolutely no bearing on this. This program could be run whilst the current directory is set to "C:\" and it can still work fine and would print out "C:\".

how is the classpath related to this.
The classpath is actually much more important to all of this. In your case the classpath would have been set to "C:\GPDesktop\workspaces\workspace_final\my_ui\maven_app\maven_app-web-app\src\main\java"  (note that it includes your projects path AND the src/main/java part but NOT the package path). With that classpath java would have been asked to execute the main class of "aa.bb.cc.dd.ee.CurrentDirectory". NOW, when Java attempts to find the class that it needs to execute, it looks at each path in the classpath (here there is only one) and then appends the package name (with '.' converted to '\') to try to find the class name with a .class extension. So it attempts to locate this file "C:\GPDesktop\workspaces\workspace_final\my_ui\maven_app\maven_app-web-app\src\main\java\com\aa\bb\cc\dd\ee\CurrentDirectory.class". Note how all of the above has no relationship to the current working directory!
0
Author Commented:
The "current directory" (also know as the "working directory") has absolutely no bearing on this. This program could be run whilst the current directory is set to "C:\" and it can still work fine and would print out "C:\".

The classpath is actually much more important to all of this. In your case the classpath would have been set to "C:\GPDesktop\workspaces\workspace_final\my_ui\maven_app\maven_app-web-app\src\main\java"

I did not set current directory anywhere. I am simply trying it in my eclipse. How to set Current Directory. Why do we need to set Current Direcory. Please advise

it looks at each path in the classpath (here there is only one) and then appends the package name (with '.' converted to '\'

I am not clear on this. Are there are any detailed links to understand this concept. please advise
0
IT Business Systems Analyst / Software DeveloperCommented:
I did not set current directory anywhere
No, you didn't. Eclipse sets it for you when it launches your application.

How to set Current Directory.
You can change the current directory that is used by right clicking on your project, choose "Run As" -> "Run Configurations...". On the screen that comes up, go to the "Arguments" tab and then at the bottom of that is the option to set the "Working Directory" that will be used to launch your application with.

Why do we need to set Current Direcory
Quite often you don't NEED to set it to anything specific. But note this, it is ALWAYS set to something, even if you don't care what it is set to, every application that you run (Java or otherwise) has a current working directory set.

I am not clear on this.
Maybe I confused you with the multiple paths part, so I will make the example even simpler, and we can go from there. Take this example command line to run a Java program...

java  -classpath  C:\Java\ExampleProject   aa.bb.cc.MyMainClass

Just so we are clear on this, all we are doing is starting a JVM, setting the classpath to "C:\Java\ExampleProject" and then asking the JVM to execute a class called "MyMainClass" that is located in the "aa.bb.cc" package. Is this all ok for the moment?   (Therefore, "aa.bb.cc.MyMainClass" is what is known as the "fully qualified class name", it is the class name but it has been "fully qualified" by specifying the package name aswell)

Now what the JVM does, in order to actually find the code that it has been asked to run, is fairly simple. It takes the classpath....

C:\Java\ExampleProject

... and appends folders as per the package name ...

C:\Java\ExampleProject\aa\bb\cc

... and then it looks in that folder for a file that has the name of the class that has been requested to be run, ie. MyMainClass, with the .class file extension. So the full path of the file that the JVM attempts to load is ...

C:\Java\ExampleProject\aa\bb\cc\MyMainClass.class

That is all that I was trying to explain in the previous post. I don't think it can get much clearer than that. I did a quick Google search and the links that I found were all much more complicated than that!
0
Author Commented:
Thank you, Let me check  Run Configuration and get back to you.
0
Author Commented:
You can change the current directory that is used by right clicking on your project, choose "Run As" -> "Run Configurations...". On the screen that comes up, go to the "Arguments" tab and then at the bottom of that is the option to set the "Working Directory" that will be used to launch your application with.

i see it shows below as default.

C:\\softwares\eclipse-juno-jee-sr-win32\eclipse

I see "other" option to choose from.
variables.jpg
0
IT Business Systems Analyst / Software DeveloperCommented:
If you click on that "Variables..." button and just select some items from the variables list, it gives you a short description of what each variable option is, and this should make it pretty obvious what they are and how they can be useful. Also, you can press "F1" key while in that dialog to get a little more information about these variables.

If you need more info than that, then this is well past the topic of your original question, and you should therefore finialise this one and then ask a new one.
0

Experts Exchange Solution brought to you by