How to acces & modify resources in Eclipse during runtime - java

I have some resources that I'm working on in an Eclipse application. I need those resources to be available for access and modification by every other plugin.
I tried to place the resources under a source folder, but that causes Eclipse to copy the resources under bin, i.e. in a jar file. Therefore I can't modify them in runtime.
My question is, where and how do I place my resources so that everyone is able to see them and the resources are available for modification?

You can use the Workspace Mechanic plugin . It allows you to export , record and import preferences based on mechanic task files. You can have LAST MOD and RECONCILE tasks. For more information : http://code.google.com/a/eclipselabs.org/p/workspacemechanic/
Hope this helps.

Here's my solution:
I created a fragment project which only contains my resources and nothing else. To prevent Eclipse from copying my resources to a .jar file on runtime, I added the following to the MANIFEST.MF of the fragment project:
Eclipse-BundleShape: dir
Bundle-ClassPath: data/,
.
("data" being the folder where my resources reside.)
Thusly my resources remain as they are, and I can modify them however I want during runtime.

Related

How to properly add resources to a java eclipse project [duplicate]

I know the file needs to be where the getClass().getResource(filename) can find it, but I don't know where that is.
I'm interested both in where to put the files on the filesystem itself, and how to go about using Eclipse's functionality to set up the resources.
For Eclipse, typically all you need to do is set up a folder somewhere within your source code directory. For instance, if the directory containing your source is /src then you can create a /src/resources folder to place your images/files in. Then, within your class you do a getResource("/resources/image.png") to retrieve it.
You can also place the image/file within the same folder/package as the class trying to access it if you wish (example: place the image.png in the com.mycompany package with the com.mycompany.Foo class that needs to access it and call getResource("image.png")), but I've found it's easier to keep resources like images and other files in their own special directory outside of the class folders -- they're just easier to manage that way.
In Eclipse, whenever you do a build, the files within this resource directory will be copied over into your build directory along with your compiled classes.
It's important to note that if you have "Build Automatically" turned on in Eclipse (as most people do) any resources in this directory that get changed outside of Eclipse (i.e. you edit an image using an image editing tool) that the IDE may not always detect this change. Usually doing a refresh on the project folder will ensure that the file gets updated in the build in these situations.
You can either put them in the src folder alongside your classes, or you can create a new source folder for the purpose (usually called resources), although you'll locate them identically from code.
Then you get at them using getResource("/com/x/y/foo.png").

Jar file contains a duplicate of every image

I am generating my jar file with Intellij Idea, and it works. However, I extracted the jar to see why it was mysteriously bigger in size then expected. I found the resources directory, containing all my images, as expected, but... for some reason, outside the resources directory, in the 'main' folder when you first open the extracted jar, are all the images again.. duplicated. This does not reflect my project structure or anything, the only place I have images are in resources. Anyone know what's going on, and how to stop this?
Project Structure:
Check the jar artifact configuration, make sure it's not set to package the contents of the resources directory inside the jar (just the compiler output entry would be enough since it will contain the resources already).
If it's not the case, check the output directory of your project. The files from the resources directory should be present inside the root of the output directory. If there is resources subdirectory, delete it and rebuild the project, ensure it doesn't appear in the output again.
In case the resources subdirectory appears in the module output after rebuild, verify the module roots configuration. It could be that the directory above resources is configured as resources (or sources) itself.
If you can't figure out the problem, please share a complete sample project illustrating it and I'll point you to the exact configuration you should change.

Maven resource folder

Hy,
I want to get the resource folder (ex: C:\Users\Raul\workspace\Serial\src\test\resources) in a Maven project but every time I run this java code:
System.out.println(getClass().getResource("").getPath());
it returns me this path: C:/Users/Raul/workspace/Serial/target/test-classes/
The last time I used Maven, worked that way without any changes from me.
Thx in advance.
With a typical setup, Maven copies the resources to the target/classes (or target/test-classes) directory. Also, the target/classes (or target/test-classes) directory is added to the classpath.
If you have a file src/test/resources/foo.txt, then you would access the file using getResource("/foo.txt").
Generally speaking, you would not want your code to refer to source folders to access resources. Resources might be put in multiple locations and it is pretty common to "filter" the resources (replace tokens with build property values). In the filtering case, you absolutely do not want the processed resource files to be in the source directory.

How would I compile a Java application including multimedia?

I wish to compile my java project in eclipse but I am at a loss and I can't figure out how to include my pictures and my database in the project or how to compile them all into one .jar file. Any help will be appreciated.
To add to what Bruno said. If you're using eclipse to build/package your project then:
add resources subfolder to project root folder
right click project -> properties -> java build path and in source tab add the folder you just created.
You can load data from InputStreams obtained from the classloader (using data on the classpath, possibly in a jar) instead of a FileInputStream (assuming that's what you do) as described here: http://download.oracle.com/javase/1.5.0/docs/guide/lang/resources.html
For the database, it will depend on whether the database engine you're using can load from the classpath. This method doesn't allow you to write, and some database engines may require write access for locking (depending on what you do with the DB).
Any file/directory you copy under the /src of your eclipse project will be included in the jar file created by eclipse and will be accessible from the java programs of that project as classpath resources.

how to filter files from the root "classes" and "test-classes" folders in Eclipse?

I am using ClearCase in my application which generates a whole load of ".copyarea.db" files (one in every folder).
These cause conflicts when publishing to Tomcat as Eclipse will bundle the "classes" and "test-classes" folders into one JAR (not sure why it does this - as there is no need to have test classes available on the application server).
Any folders with the same names will have a separate .copyarea.db in the classes and test-classes branches.
I managed to get around this problem in general by adding ".copyarea.db" to the Filtered resources on the Java->Compiler->Building->Output Folder preference page. This stops the file appearing in source output (package/class folders), the vast majority of cases.
However there remains the problem of the root folder, i.e. "target/classes/.copyarea.db" and "target/test-classes/.copyarea.db".
These files are not filtered as they are not part of the compile task.
Just deleting the files manually doesn't help either, as Eclipse expects to find them and doesn't.
How can I exclude these ".copyarea.db" files from the root "classes" and "test-classes" folders?
This file .copyarea.db is created in each directory of a web view (through CCRC, the remote client of ClearCase).
The simplest solution would be to not put under source control the directories classes and test-classes.
Then, as illustrated by this technote:
Otherwise, the directory should not be controlled.
If the directory is not controlled, then the .copyarea.db file within it should never be created.
If the directory was accidentally added to source control, this could be prevented in the future by adding classes to the ignore list. This would avoid the directory being passed to ClearCase during the Share.
Since you don't usually version the binaries produced under classes and test-classes, removing those folder, and adding them again manually (keeping them private to your view) is an acceptable solution.
If those folders are not under version control, then those copyarea.db come from versioned folders which contain them, and have been copied to the destination folder.
In that case, you can simply remove the copyarea.db files.
But since you said that Eclipse "expects to find them and doesn't", I assume those "classes" and "test-classes" folders are not private folder.
concerning
These cause conflicts when publishing to Tomcat as Eclipse will bundle the "classes" and "test-classes" folders into one JAR (not sure why it does this - as there is no need to have test classes available on the application server)
you can configure this in project properties, build path, order and export, or use maven and m2eclipse!

Categories

Resources