Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

current path  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
how can I have the absolute path where the current class is executed?

(if the class is packed in a .jar file, without the jar file name).

Thank you for answering.
 
Rancher
Posts: 1776
 
Sheriff
Posts: 23510
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean you want the current working directory, or the directory where the executable jar is located? It might also help to explain why you need to find whichever of those two things you meant, as this is quite likely a question based on some decision you already made. It's possible that was a bad decision.
 
Jonathan Sullivan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Do you mean you want the current working directory, or the directory where the executable jar is located? It might also help to explain why you need to find whichever of those two things you meant, as this is quite likely a question based on some decision you already made. It's possible that was a bad decision.



I mean the directory where the executable jar is located.

I would like to do this:

I have a file that I put in the package I'm using;
I would like to copy that file in another location.

So the problem is this one:

when I export my classes in a jar file and I execute it on another computer, the path of the file is no more the same.
Indeed I would like to use the path of this file in a portable way; also after the packaging a jar.

So I'm thing about how to have the absolute path of the running jar, and then the path of the file in it.

The better solution I found until now is this one:



where MyClass is my class name.
In this way I have the absolute path of the class running (or of the jar file).

But I don't have yet any reference to the file packed into the jar.
In addition I would like to parametrize"MyClass" in a way that I don't have to typing the name of the class.

Any help? :banghead:


 
Jonathan Sullivan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Jai wrote:Try this http://www.coderanch.com/t/384661/java/java/find-physical-path-current-java

and please SearchFirst



with this one I have the current working directory.
And it is not that I would like to have.

It is possible to have it also with:



or



But is not what I need.
 
Author and ninkuma
Marshal
Posts: 66685
167
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You did not answer the following:

Paul Clapham wrote:It might also help to explain why you need to find whichever of those two things you meant, as this is quite likely a question based on some decision you already made. It's possible that was a bad decision.

 
Marshal
Posts: 59131
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds too difficult for "beginning", so I shall move this question.
 
Jonathan Sullivan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You did not answer the following:

Paul Clapham wrote:It might also help to explain why you need to find whichever of those two things you meant, as this is quite likely a question based on some decision you already made. It's possible that was a bad decision.



Hello Bear Bibeault,
on 2 posts above I tried to explain what I'm searching to doing.

I'm trying to have an absolute reference to a file
(but a relative maybe a better choice),
but I don't know how.
 
Paul Clapham
Sheriff
Posts: 23510
47
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jonathan Sullivan wrote:But I don't have yet any reference to the file packed into the jar.



Aha. So your jar includes a data file and you need access to it? Okay. Then deciding you needed to find the directory where the jar was located was indeed the wrong decision. The way to do it is like this:

where you provide the path inside the jar to the file. You don't need to know where the jar is, only that it is in the classpath.

And you said you wanted to copy that file from the jar to some other location? Then, again, the directory where the jar is located is not the best choice. It's better to store it in the user's home directory, or perhaps a subdirectory of that which is specific to your application. You get that like this:

which on the system I'm working on right now returns

C:\Users\clapha-p

 
Jonathan Sullivan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answers.

I will try your solutions.
 
Jonathan Sullivan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Jonathan Sullivan wrote:But I don't have yet any reference to the file packed into the jar.



Aha. So your jar includes a data file and you need access to it? Okay. Then deciding you needed to find the directory where the jar was located was indeed the wrong decision. The way to do it is like this:
[...]



Thank you for your answers...
I have almost what I need.

But I have an error:



I have always this Exception:
Exception in thread "main" java.lang.NullPointerException

here:

while((len=inputStream.read(buf))>0)

Any suggestions?

Thank you!
 
Jonathan Sullivan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jonathan Sullivan wrote:

But I have an error:

[...]

I have always this Exception:
Exception in thread "main" java.lang.NullPointerException




Solved! :jumpingjoy:

It was a path problem!

Thank you!
 
this is supposed to be a surprise, but it smells like a tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!