aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes how to get the java code path in RunTime. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "how to get the java code path in RunTime." Watch "how to get the java code path in RunTime." New topic
Author

how to get the java code path in RunTime.

jing hu
Ranch Hand

Joined: Jun 30, 2009
Posts: 63
Now In my program, I need to read some data from info.txt

Application
----src/test.java
----bin/test.class
----info.txt


So in test.java . I can write the code like this:


But I need to use Ant my program.When use ant to execute these code. It will search the info.txt in the ant-build.xml main directory not in the application directory.
the build.xml don't in the application directory. So it will not search the info.txt.

Now I want to modify the

How can I modify the code to achieve my goal.
Thank you in advance.
Any comments will be popular
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
What happens if you try "info.txt"?
jing hu
Ranch Hand

Joined: Jun 30, 2009
Posts: 63
Campbell Ritchie wrote:What happens if you try "info.txt"?

just now I test it.
It can not find the "info.txt".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
You get a totally different folder structure when you use an IDE from what you get when you use a command line. Why have you got a bin folder? Have you put your own code in the Java installation folders? I created a folder called src with this class in, and an info.txt file. In its parent folder, I put another info.txt file. The package declaration means there will be a "trivia" folder as a child of "src".
campbell@queeg:~/java/src$ gedit test.java&
[1] 4452
campbell@queeg:~/java/src$ javac -d . test.java
[1]+ Done gedit test.java
campbell@queeg:~/java/src$ java trivia.test info.txt
info in src
campbell@queeg:~/java/src$ java trivia.test ./info.txt
info in src
campbell@queeg:~/java/src$ java trivia.test ../info.txt
info.txt in the root folder
Note the output, and note also that I am using a Linux box.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Output when I created a "src" folder on a floopy disc and executed the same Java code on a Windows box.
A:\src>dir
Volume in drive A has no label.
Volume Serial Number is 0000-0000

Directory of A:\src

08/07/2009 13:08 <DIR> .
08/07/2009 13:08 <DIR> ..
08/07/2009 13:14 13 info.txt
08/07/2009 13:16 879 test.java
2 File(s) 892 bytes
2 Dir(s) 1,430,528 bytes free

A:\src>javac -d . test.java

A:\src>java trivia.test info.txt
info in src

A:\src>java trivia.test ./info.txt
info in src

A:\src>java trivia.test ../info.txt
info.txt in root folder

A:\src>dir
Volume in drive A has no label.
Volume Serial Number is 0000-0000

Directory of A:\src

08/07/2009 13:08 <DIR> .
08/07/2009 13:08 <DIR> ..
08/07/2009 13:14 13 info.txt
08/07/2009 13:16 879 test.java
08/07/2009 13:16 <DIR> trivia
2 File(s) 892 bytes
3 Dir(s) 1,428,992 bytes free
It is virtually identical. I suspect you have a problem with your folder structure.
jing hu
Ranch Hand

Joined: Jun 30, 2009
Posts: 63
Campbell Ritchie, Thank you.
I want to post some code to express my intention clearly.
TestPath
src/Test.java
bin/Test.class
info.txt
build.xml
newAnt/build.xml

Test.java




build.xml


newAnt/build.xml



In the test.java, it will read file from the directory TestPath.
So if when I run test.java it will search the "info.txt" successfully.
And in the directory TestPath build.xml, it will call the test.main directly. It will also find the "info.txt". But when I run the build.xml in the newAnt Folder to invoke the build.xml in the directory TestPath. For this folder, the symbol "." means the directory newAnt not the TestPath. So it will not find the "info.txt"

I want to modify the code to run two build.xml files successfully.

If I can get the test.java runtime location. I can modify the code like this.

So It will be ok for the two build.xml files.
If you know how to get the location, tell me.
Thank you very much.


Sona Patel
Ranch Hand

Joined: Mar 30, 2009
Posts: 75
Hi Jing...

if you try with following code. It will look for the file in class path. So you can set your classpath before running application to point to the location where this file is stored.

FileReader file = new FileReader("info.txt");

The other possible way is
String path = System.getProperty("user.dir");

You can find more information from http://www.java2s.com/Tutorial/Java/0120__Development/UsingSystemgetPropertyStringkeytoretrievesystemproperties.htm

Regards...
jing hu
Ranch Hand

Joined: Jun 30, 2009
Posts: 63
Sona Patel wrote:

The other possible way is
String path = System.getProperty("user.dir");



hi Sona.
if you try the code

path will be variational when I using different build.xml to invoke the code.

So it will not satisfy my requirement.
Any way thanks.
And I don't how to upload the attachment.

when upload the attachment, it will show that:Files with the extension .x are not allowed as attachment in the message. I want to know what suffix can be uploaded???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
I think this is no longer a "Java in General" question. Moving thread. Difficult to be sure where to move it: let's try the ANT forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get the java code path in RunTime.