• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

current path

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this https://coderanch.com/t/384661/java/java/find-physical-path-current-java

and please SearchFirst
 
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:Try this https://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.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic