Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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"));
    }
}

Open in new window





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.

Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
Avatar of mccarl
mccarl
Flag of Australia image

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!
Avatar of gudii9

ASKER

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
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!
Avatar of gudii9

ASKER

Thank you, Let me check  Run Configuration and get back to you.
Avatar of gudii9

ASKER

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.
I see "variables" button there. How variable related/useful there. please advise
variables.jpg
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial