javaeclipsecommand-promptcwd

Fiddly thing to do with \bin directory in Eclipse


This has really got me baffled.

In my Eclipse workspace I have a project called "Java scratchpad". In "Package Explorer" you see "src" (Source Folder) under this and then "root" (Package). Under "root" is a .java file called "LoggingTest.java".

So the path to this java file is "G:\My Documents\software projects\workspace\Java scratchpad\src\root\LoggingTest.java".

When I run the following code in Eclipse:

        Path currentRelativePath = Paths.get("");
        String s_currRelPath = currentRelativePath.toAbsolutePath().toString();
        String pathWithForwardSlashes = s_currRelPath.replace( "\\", "/" );
        System.out.println( "path " + pathWithForwardSlashes );

The result is

path G:/My Documents/software projects/workspace/Java scratchpad

But... when I am running the same piece of code at the Command Prompt I have to start in the following directory:

G:\My Documents\software projects\workspace\Java scratchpad\bin

... and then go > java root.LoggingTest at the prompt.

The output from the above bit of code is then:

path G:/My Documents/software projects/workspace/Java scratchpad/bin

In other words, when you run the thing at the Command Prompt you are running a .class file under the \bin directory, but when you run in Eclipse, the Eclipse framework "pretends" that the bin directory doesn't exist.

And the upshot is that I get different values for the "current relative path" (CWD) depending on whether I'm running in Eclipse or at the Command Prompt. If I want to make or use a directory relative to the CWD I'm going to get different values depending on whether I'm running in Eclipse or at the Command Prompt.

I'm feeling quite slow tonight. What should I do? Is there a way of detecting whether a project is being run in Eclipse or at the Command Prompt? Or should I simply try to detect whether the path ends in "\bin" and remove those four characters to make the paths equivalent?

later

Just adding a note in reply to the comment from E-Riz:

It's a simple thing: I want to create logs and I want to create Lucene indices... and to keep things in one location (at this stage anyway) I just want to put this output in a location such as ...\Java scratchpad\output\indices\[name of index] or, respectively, ...\Java scratchpad\output\logging\[name of log].

And, yes, of course at present I do know the absolute path involved here... however, as the name suggests, this is a "scratchpad" or test area... so then this code which uses the CWD to determine where to put or find logs or indices can be used more generically, without having to know the absolute paths involved.

But it has to work either in Eclipse or at the command prompt...!

NB my current workaround is indeed to check whether pathWithForwardSlashes ends in "\bin", and if so to delete these last 4 chars. I can't be the only person to have encountered this oddity. Bet there are cleverer solutions!


Solution

  • There's a couple of facts that you should know or keep in mind:

    So, you have a couple of options to make things consistent: use a script/batch to run your app on the command-line, specifying -cp so the JVM knows where your class files are (and any other JARs it might need down the road, too); or, reconfigure your Eclipse launch to match where you run from on the command line. I think the (by far) preferable option is the first one.

    Having said all that, you should usually not need to do any path manipulation at all when it comes to files/resources in Java. Everything is relative to either the current working directory where Java was run from, or the classpath.