Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

app path

Thread view: 
Chameleon - 19 Aug 2007 19:30 GMT
I want to know in which path is application.
Until now I use the following super_dumb code:
application.jar          : the jar file
subtitle/MainFrame.class : the main() class

try {
  // get application path
  rootPath =
URLDecoder.decode(ClassLoader.getSystemResource("subtitle/MainFrame.class").getPath().replaceAll("(application\\.jar!/)?subtitle/MainFrame\\.class$|^(file\\:)?/",
""), "UTF-8");
} catch (UnsupportedEncodingException ex) {
  rootPath = "";
}

Is there a better approach?
Thanks!
Daniel Pitts - 19 Aug 2007 21:09 GMT
> I want to know in which path is application.
> Until now I use the following super_dumb code:
[quoted text clipped - 13 lines]
> Is there a better approach?
> Thanks!

What's your ultimate goal? I don't see any motivation for knowing
where the current class file is from, perhaps if you gave use your
motivation, we could suggest an even better approach
Chameleon - 20 Aug 2007 00:03 GMT
>> I want to know in which path is application.
>> Until now I use the following super_dumb code:
[quoted text clipped - 17 lines]
> where the current class file is from, perhaps if you gave use your
> motivation, we could suggest an even better approach

My goal:
I have the "application.jar" inside a directory, for instance (windows)
C:\Program Files\My Application

I want a String with value: "C:\Program Files\My Application".
With the code above I have it. But code is a little bit confusing.
Andrew Thompson - 20 Aug 2007 04:25 GMT
>>> I want to know in which path is application.
>>> Until now I use the following super_dumb code:
[quoted text clipped - 7 lines]
>
>I want a String with value: "C:\Program Files\My Application".

Though it might be a strategy that you think
might achieve some goal - it is *not* a goal in
and of itself.

Try to think of the 'goal' as the 'X' of..
"I want to offer the *end* *user* 'X'".

What is the 'X'?

>With the code above I have it. But code is a little bit confusing.

So is your approach.  There are very few end goals
that require knowing the codebase that the application
is using, and attempts to find that codebase often
indicate poor (and fragile) design.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Chameleon - 20 Aug 2007 23:19 GMT
O/H Andrew Thompson έγραψε:
>>>> I want to know in which path is application.
>>>> Until now I use the following super_dumb code:
[quoted text clipped - 15 lines]
>
> What is the 'X'?

you right.
1. I want to open a "config.ini" file which is in JAR's path.
2. I want to open an "index.html" which is in JAR's path.

>> With the code above I have it. But code is a little bit confusing.
>
> So is your approach.  There are very few end goals
> that require knowing the codebase that the application
> is using, and attempts to find that codebase often
> indicate poor (and fragile) design.

a better solution, but it works ONLY for JAR's and not for unpacked classes:

// get application path
try {
  rootPath = new File(System.getProperty("java.class.path")).
                    getCanonicalFile().getParent() + File.separator;
} catch (IOException ex) {
  rootPath = "";
}
Andrew Thompson - 21 Aug 2007 00:03 GMT
>O/H Andrew Thompson έγραψε:
>>>>> I want to know in which path is application.
...
>> What is the 'X'?
>
>you right.
>1. I want to open a "config.ini" file which is in JAR's path.
>2. I want to open an "index.html" which is in JAR's path.

That *still* does not quite come up to a statememnt
of a 'user feature', but it is close enough.

OK - *why* are these files in the Jar's path?
Who, or what, caused them to be there?

For example, some apps. are supplied as Zip
archives, with the instructions to 'unzip to a new
directory on your system' - that might be one way
these files all happen to be in the same place.

But is does not *have* to be that way.
You might write a simple 'unzipper - installer'
that puts the config.ini and index.html into
a *known* path, that can be easily found later,
while putting everything else in a single (hidden
or otherwise) directory.

Web start also offers mechanims to 'install'
files or do other such things at first application
start.

>>> With the code above I have it. But code is a little bit confusing.

..and 'fragile'.  Every time devlopers seem to come
up with a 'rock solid' way to get the app. path, Sun
(seems to*) changes the rules.  * They are really just
trying to design a robust and fast plug-in, but things
happen in that process that affect Java apps.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Chameleon - 21 Aug 2007 15:21 GMT
O/H Andrew Thompson έγραψε:
>> O/H Andrew Thompson έγραψε:
>>>>>> I want to know in which path is application.
[quoted text clipped - 6 lines]
> That *still* does not quite come up to a statememnt
> of a 'user feature', but it is close enough.

"config.ini" keeps the application's configuration (Settings dialog)
"index.html" is the file which load in browser as HELP with
"BrowserLauncher" class and it is outside the jar because not all
browsers support the "jar:http://......."

> OK - *why* are these files in the Jar's path?
> Who, or what, caused them to be there?

I want "config.ini" in the directory where I place the JAR file.
I want "index.html" and some other help files, outside the JAR file
because not all web browsers can open these files inside the JAR.

> For example, some apps. are supplied as Zip
> archives, with the instructions to 'unzip to a new
> directory on your system' - that might be one way
> these files all happen to be in the same place.

My application installation package is a zip file contains a JAR file
and some help files.
Or a JAR file only (forget about help).

> But is does not *have* to be that way.
> You might write a simple 'unzipper - installer'
> that puts the config.ini and index.html into
> a *known* path, that can be easily found later,
> while putting everything else in a single (hidden
> or otherwise) directory.

Yes. "config.ini" created inside directory which contains the JAR file
and "index.html" and other help files are placed in the same directory
plus "/help/"

> Web start also offers mechanims to 'install'
> files or do other such things at first application
> start.

I believe the problem is very easy.

>>>> With the code above I have it. But code is a little bit confusing.
>
[quoted text clipped - 3 lines]
> trying to design a robust and fast plug-in, but things
> happen in that process that affect Java apps.

the code works from version 1.4 to today. I don't know keyword "java"
before the version 1.4! I am newbie ;-)

Sorry for the "<present simple> *before* ..." and other related things.
My english sucks!
Daniel Pitts - 21 Aug 2007 01:43 GMT
> O/H Andrew Thompson       :
>
[quoted text clipped - 39 lines]
>    rootPath = "";
> }

Ah, perhaps you should look into ClassLoader.getResource(), or
Class.getResource(). That is probably what you are really looking
for.
Arne Vajhøj - 20 Aug 2007 01:45 GMT
> I want to know in which path is application.
> Until now I use the following super_dumb code:
[quoted text clipped - 11 lines]
>
> Is there a better approach?

I don't think so.

I use the following rather similar code:

    private String getPath(Class cls) {
        String cn = cls.getName();
        String rn = cn.replace('.', '/') + ".class";
        String path =
getClass().getClassLoader().getResource(rn).getPath();
        int ix = path.indexOf("!");
        if(ix >= 0) {
            return path.substring(0, ix);
        } else {
            return path;
        }
    }

Arne
Real Gagnon - 20 Aug 2007 03:08 GMT
> I want to know in which path is application.
> Until now I use the following super_dumb code:
[quoted text clipped - 13 lines]
> Is there a better approach?
> Thanks!

Try this :

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class FromWhere {
 public static void main(String args[]) throws Exception{
   FromWhere s = new FromWhere();
   s.getHome();
 }

 public void getHome() throws IOException, URISyntaxException{
   URL u =
     getClass().getProtectionDomain().getCodeSource().getLocation();
   File f = new File(u.toURI());
   System.out.println(f.getParent());
 }
}

Bye.
Signature

Real Gagnon  from  Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.