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 / Tools / November 2003

Tip: Looking for answers? Try searching our database.

Eclipse vs. GetResourse

Thread view: 
P.Hill - 22 Oct 2003 23:24 GMT
I have the following code in an application:
        URL url = Thread.currentThread().getContextClassLoader()
            .getResource( "mapping.xml");
        String path = url.getPath();
The resulting path is
"C:/dev/workspace/THISPROJECT/bin/mapping.xml"
                              ***
But I would like it to be:
"C:/dev/workspace/THISPROJECT/src/mapping.xml"
                              ***
since I am playing with this file.
Or even          THISPROJECT/ini

What do I do to
1. My code
or
2. Eclipse settings
to get the path to be somewhere other than /bin/?

TIA,
-Paul
David Rabinowitz - 23 Oct 2003 12:11 GMT
add the source path to the library path.

Your problem is that the classpath contains the vin directory (with all the
class files) and not the source (java files).

David

> I have the following code in an application:
>          URL url = Thread.currentThread().getContextClassLoader()
[quoted text clipped - 17 lines]
> TIA,
> -Paul
P.Hill - 23 Oct 2003 16:58 GMT
But now I realize that it was finding one in bin because
I had left one there.

IF I DELETE THIS EXTRA COPY, IT DOES NOT FIND THE ONE IN
/SRC, in fact it doesn't find any.

Is there some other setting about using /src which I have missed?

> add the source path to the library path.
>
> Your problem is that the classpath contains the vin directory (with all the
> class files) and not the source (java files).

/src isn't shown in the library list.
But if you look under:
Project | Properties | Order and Export
You see
/MYPROJECT/src as the 1st entry with the
it checked (for export I presume).

And if you actually add the src dir to the classpath
(in Project | Properties | Libraries ) it complains
(in the little window above the list)
"classpath contains duplicate entries."
so you can't actually do that.

>>The resulting path is
>>"C:/dev/workspace/THISPROJECT/bin/mapping.xml"
>>                               ***
>>But I would like it to be:
>>"C:/dev/workspace/THISPROJECT/src/mapping.xml"
>>                               ***

-Paul
David Rabinowitz - 26 Oct 2003 14:37 GMT
add a /lib directory (good for external jars as well) and add it to the
classpath.

> But now I realize that it was finding one in bin because
> I had left one there.
[quoted text clipped - 30 lines]
>
> -Paul
Raymond DeCampo - 26 Oct 2003 22:55 GMT
> I have the following code in an application:
>         URL url = Thread.currentThread().getContextClassLoader()
[quoted text clipped - 17 lines]
> TIA,
> -Paul

This is an example of how IDEs can be evil.

After you get it working in eclipse have you given any thought to how it
is going to work outside of eclipse?

Ray
P.Hill - 28 Oct 2003 20:43 GMT
>> I have the following code in an application:
>>         URL url = Thread.currentThread().getContextClassLoader()
[quoted text clipped - 10 lines]
> After you get it working in eclipse have you given any thought to how it
> is going to work outside of eclipse?

Sure, the answer seems obvious. I place the xml in the class path of
the application which might include placing it inside a
jar file.

Does that not work for you?

The problem is not with the idea of setResource(), but with
what Eclipse is doing with its classpath while debugging.

The previous responder suggested a resonable solution:
create a PROJECT/otherstuff_for_project directory and add that
to the path.

Do you forsee a problem with either the method of working with the
IDE or placing the resulting file somewhere on the runtime class path?

-Paul
Raymond DeCampo - 29 Oct 2003 02:42 GMT
>>> I have the following code in an application:
>>>         URL url = Thread.currentThread().getContextClassLoader()
[quoted text clipped - 28 lines]
> Do you forsee a problem with either the method of working with the
> IDE or placing the resulting file somewhere on the runtime class path?

My apogolies.  I had mistaken you for someone who did not know how to
function outside of their IDE.  Of course, if you understand that all
that is necessary is to place the xml file in the CLASSPATH, why is this
so difficult inside the IDE?  Do you really have to create another
project to do that?  It seems like the IDE is working against you...

I guess that wouldn't surprise me too much.  I soured on eclipse the
first time I wanted to just open a file that wasn't in my project.  It
turned out to be impossible.

Don't get me wrong; IDEs have there place and are not inherently evil.
It just seems like a lot of work to have to continue to configure them
after you've already configured an ANT script to do everything and
configured your app to work when installed.  I'm waiting for an IDE
whose configuration is based on ANT ;)

Ray
P.Hill - 29 Oct 2003 04:02 GMT
>> Do you forsee a problem with either the method of working with the
>> IDE or placing the resulting file somewhere on the runtime class path?
>>
> My apogolies.  I had mistaken you for someone who did not know how to
> function outside of their IDE.

Hmm, how would such an imagined person install their work?  Whatever.

> Of course, if you understand that all
> that is necessary is to place the xml file in the CLASSPATH, why is this
> so difficult inside the IDE?

Why are you asking me? I am the one who asked that question. :-)

> Do you really have to create another
> project to do that?

Ouch!  No one is suggesting another project!!!!
In fact the solution suggested is exactly the solution
you would use when working with a make or ant file which cleans
the output directory.  I had original tried the obvious
of placing other files in the src tree, but somehow the IDE
wasn't too keen on this or I don't see the right switch, which
is why I quieried c.l.j.softwaretools.  I
think there is some bug involved because the complaint
suggests the src IS (or should be) in the classpath already.
You might count this as the IDE working against me, but just like
a complex make or ANT file, what the IDE expects to do (like
hose out the bin dir when rebuilding) is just something you
have to know what it wants to do.

> I guess that wouldn't surprise me too much.  I soured on eclipse the
> first time I wanted to just open a file that wasn't in my project.  It
[quoted text clipped - 5 lines]
> configured your app to work when installed.  I'm waiting for an IDE
> whose configuration is based on ANT ;)

Eclipse can invoke any ANT task. It is highly integrated with
ANT.  If your ANT tasks does all the building you can forget
about worrying about telling the project the same thing, but still
use the interface to select the ant task, set your favorite,
debug what you've built etc.

Maybe you should investigate how ANT works with Eclipse, I found it
very usable.

But back to putting an extra file in the right place.

-Paul
Raymond DeCampo - 30 Oct 2003 00:22 GMT
>>> Do you forsee a problem with either the method of working with the
>>> IDE or placing the resulting file somewhere on the runtime class path?
[quoted text clipped - 3 lines]
>
> Hmm, how would such an imagined person install their work?  Whatever.

The same way I imagined that they are solving their current problem,
posting to newsgroups or finding someone else who knows. :)

>> I guess that wouldn't surprise me too much.  I soured on eclipse the
>> first time I wanted to just open a file that wasn't in my project.  It
[quoted text clipped - 14 lines]
> Maybe you should investigate how ANT works with Eclipse, I found it
> very usable.

Yes, Eclipse can invoke any ANT task; that's pretty minimal.

However, that does not mean that "[Eclipse] is highly integrated with
ANT."  For example, Eclipse has a compile-on-the fly feature that
provides immediate feedback when creating code.  However, one must
configure the CLASSPATH and other build options separately from the ANT
script for this to work.  Why can't it just use the ANT script I have
already carefully crafted?  Imagine if it did...then you would be able
to instantly "configure" eclipse for any project that already has an ANT
 script, e.g. any project from Apache.

Don't get me wrong, Eclipse is a fine tool and is progressing nicely.
Also, I do not know of any tool that does what I propose above, so I
can't really fault Eclipse for not doing it.  It's just what I want out
of an IDE.

Ray
P.Hill - 02 Nov 2003 06:34 GMT
> However, that does not mean that "[Eclipse] is highly integrated with
> ANT."  For example, Eclipse has a compile-on-the fly feature that
[quoted text clipped - 4 lines]
> to instantly "configure" eclipse for any project that already has an ANT
>  script, e.g. any project from Apache.

We have an open source IDE with a published API for plugins, books
that tell us how to make plugins, readable XML files that contain
all of Eclipses configuration information and Ant Scripts which
are also XML. Sounds like a great project for someone!  All we
need is a configuration dialog to tell it how to rip apart the
ant script and how to combine any IDE symbols (i.e. JUNIT_LIB etc.)
it finds in the ant and we're in business.

Now someone will have to help the IDE user and plugin builder
with the gotcha of an ANT task which uses an explicit library
or even more interesting an ANT task which uses various
groups of libraries for various tasks.  Obviously the right
group is the one used for compiles, but what if the plugin
finds two compiler invocations with different library
definitions!  Oh NO!

Obviously the tool should also configure the IDE project to
link any external dirs also.  Given a bit of thought I sure
there are other things I would make the plugin do with an
ant file.

> Don't get me wrong, Eclipse is a fine tool and is progressing nicely.
> Also, I do not know of any tool that does what I propose above, so I
> can't really fault Eclipse for not doing it.  It's just what I want out
> of an IDE.

What are doing next weekend?  What am I doing next weekend? :-)
Actually I saw this done in JBuilder with good success, I'm
sure it is doable in Eclipse.

It will probably be done soonm maybe someone has done it.
Oh the joys of open source!

Cheers,
-Paul
Dale King - 04 Nov 2003 17:59 GMT
> >>> Do you forsee a problem with either the method of working with the
> >>> IDE or placing the resulting file somewhere on the runtime class path?
[quoted text clipped - 36 lines]
> to instantly "configure" eclipse for any project that already has an ANT
>   script, e.g. any project from Apache.

Because ANT does not handle dependecies reliably. It has some insufficient,
ad hoc hacks to try to handle simple dependencies, but still will not
guarantee to rebuild everything. Eclipse in my experience is 100% reliable
in this regard (as long as it knows about changes, which means changes made
outside of Eclipse require a refresh).

--
Dale King
Dale King - 29 Oct 2003 16:48 GMT
> I have the following code in an application:
>          URL url = Thread.currentThread().getContextClassLoader()
[quoted text clipped - 14 lines]
> 2. Eclipse settings
> to get the path to be somewhere other than /bin/?

You seem to be a little confused with how Eclipse works here. I assume from
your post that you have your project properly set up with src as a source
directory and bin as the output directory.

When Eclipse builds it will compile the java files in your src directory and
put them into your bin directory, but it will also copy any other files in
the src directory such as xml files, property files, and graphics to the bin
directory. At runtime you shouldn't be accessing the file in the src
directory. Anything in the class path for the project besides other jars and
projects should be in the bin directory.

If you are trying to write to the file then you shouldn't be using
getResource as that is designed for reading.

If you have files in ini that you need to access as a resource then you
probably need to make ini a source directory or move the ini folder into the
src folder.
--
Dale King
P.Hill - 02 Nov 2003 06:19 GMT
>>I have the following code in an application:
>>         URL url = Thread.currentThread().getContextClassLoader()
[quoted text clipped - 18 lines]
> your post that you have your project properly set up with src as a source
> directory and bin as the output directory.

Yup, that's me.  I not trying any tricks.

> When Eclipse builds it will compile the java files in your src directory and
> put them into your bin directory, but it will also copy any other files in
> the src directory such as xml files, property files, and graphics to the bin
> directory.

Oh, very interesting!

> At runtime you shouldn't be accessing the file in the src
> directory.

Didn't want to access it in the src really, just wanted the code to
find the latest .ini file.

> Anything in the class path for the project besides other jars and
> projects should be in the bin directory.

So should I have them in the src and let them be copied or should
I place them in the bin dir?  When you say "should have" is that the result of
my starting with them there, some setting to say 'sure go ahead and move them
there just before running' or because of some built-in behavior?  Is there
some list it recognizes as 'to be copied' before running?

> If you are trying to write to the file then you shouldn't be using
> getResource as that is designed for reading.

I'm certainly not writing to it in Java!  Sorry If I wasn't clear.  When
I said I was "playing" with the file, I meant I was making various entries
in the INI at development time with an editor to make the program work at
runtime.  The 'playing' involved maintainomg and extending whatever
was in the INI file not some unorthodox feedback loop involving writing
back an INI file at runtime.

> If you have files in ini that you need to access as a resource then you
> probably need to make ini a source directory or move the ini folder into the
> src folder.

And then I don't need to put /src/ini (or /resource) in the CLASSPATH?
When you say 'make it a source dir.' what do mean with regard to Eclipse?

Sorry about the confusion, I was using ant scripts in JSP projects
to put everything together for runtime in my measly attempts in Eclipse
before, so am not familiar with the default behavior a plain Java
application with regard 'other files.'

thanks for your help,
-Paul
Dale King - 04 Nov 2003 18:18 GMT
> >>I have the following code in an application:
> >>         URL url = Thread.currentThread().getContextClassLoader()
[quoted text clipped - 42 lines]
> there just before running' or because of some built-in behavior?  Is there
> some list it recognizes as 'to be copied' before running?

Yes, you should put them in a source folder and it will get copied to the
bin directory. That is built-in behavior of the JDT. See this quote from the
FAQ for the JDT:

"If your Java project is using source folders, then in the course of
compiling the source files in the project, the Java compiler copies non-Java
resources to the output folder as well so that they will be available on the
class path of the running program. To avoid certain resources to be copied
to the output location you can set a resource filter in the compiler
preferences: Window > Preferences > Java > Compiler > Build Path"

> > If you are trying to write to the file then you shouldn't be using
> > getResource as that is designed for reading.
[quoted text clipped - 5 lines]
> was in the INI file not some unorthodox feedback loop involving writing
> back an INI file at runtime.

Then getResource is probably what you want.

> > If you have files in ini that you need to access as a resource then you
> > probably need to make ini a source directory or move the ini folder into the
> > src folder.
>
> And then I don't need to put /src/ini (or /resource) in the CLASSPATH?

Correct.

> When you say 'make it a source dir.' what do mean with regard to Eclipse?

Yes, either put it into the existing source directory or else you can make
the existing directory an additional source directory. You can have multiple
source directories. You might want your resources separate from your Java
source files. I personally don't, but it is up to you.

> Sorry about the confusion, I was using ant scripts in JSP projects
> to put everything together for runtime in my measly attempts in Eclipse
> before, so am not familiar with the default behavior a plain Java
> application with regard 'other files.'

--
Dale King


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.