Exporting an JAR file in Eclipse and referencing a file - eclipse

I have a project with a image stored as a logo that I wish to use.
URL logoPath = new MainApplication().getClass().getClassLoader().getResource("img/logo.jpg");
Using that method I get the URL for the file and convert it to string. I then have to substring that by 5 to get rid of this output "file:/C:/Users/Stephen/git/ILLA/PoC/bin/img/logo.jpg"
However when I export this as a jar and run it I run into trouble. The URL now reads /ILLA.jar!/ and my image is just blank. I have a gut feeling that it's tripping me up so how do I fix this?
Cheers

You are almost there.
Images in a jar are treated as resources. You need to refer to them using the classpath
Just use getClass().getResource: something like:
getClass().getResource("/images/logo.jpg"));
where "images" is a package inside the jar file, with the path as above
see the leading / in the call - this will help accessing the path correctly (using absolute instead of relative). Just make sure the path is correct
Also see:
How to includes all images in jar file using eclipse

See here: Create a file object from a resource path to an image in a jar file
String imgName = "/resources/images/image.jpg";
InputStream in = getClass().getResourceAsStream(imgName);
ImageIcon img = new ImageIcon(ImageIO.read(in));
Note it looks like you need to use a stream for a resource inside an archive.

Related

Difficulty using BufferedImage in Eclipse

I have stored an image in a Resource folder 'Images':
src
-com.program
-Images
In the program I use
BufferedImage image =ImageIO.read(getClass().getResourceAsStream("/myImage.png"));
to import the image.
This works fine. However, if i change the name of the image at the source(say to myImage1.png)
and try to execute
BufferedImage image =ImageIO.read(getClass().getResourceAsStream("/myImage1.png"));
I get Input==Null.
I've been try to get this to work for a while and tried various suggestions on other threads.
Any ideas?
Thanks!
The problem was most likely:
The image was in your src folder inside your project, but when the program runs, it runs from another folder containing your compiled classes. This folder did not contain the png, so you get the input == null exception (getClass().getResourceAsStream(...) returns null when resources cannot be resolved).
To make it work, you need to mark the images folder a resource folder (using Eclipse, Maven or favorite build tool), and make sure that the contents of that folder is on your class path when the program is run.

Private assets in Play 2.1

In a Play 2.1 application, where is the proper place to store private assets?
By "private asset", I mean a data file that is used by the application but not accessible to the user.
For example, if I have a text file (Foo.json) that contains sample data that is parsed every time the application starts, what would be the proper directory in the project to store it?
Foo.json needs to be included in the deployment, and needs to be uniformly accessible from the code in both development and production.
Some options:
Usually the files goes to conf folder. ie: conf/privatefiles/Foo.json
If they are subject of often change you can consider adding to your application.conf path to the external folder somwhere in the filesystem (full path), in such case you'll be able to edit the content easily without redeploying the apps: /home/scrapdog/privatefiles/Foo.json
You can store them in database as well, benefits are the same as in previous option - easy editing.
In all cases consider using memory cache to avoid reading it from filesystem/database every time when required.
I simply use a folder called data at the application root. You can use the name you want or better, store the actual name in the configuration file.
To resolve its path, I use the following snippet:
lazy val rootPath = {
import play.api.Play.current
play.api.Play.application.path.getPath
}
lazy val dataPath = rootPath + "/data/"
You can do what I did, I got the answer from #Marius Soutier here. Please upvote his answer there if you like it:
You can put "internal" documents in the conf folder, it's the equivalent to resources in standard sbt projects.
Basically create a dir under conf called json and to access it, you'd use Play.resourceAsStream(). Note that this gives you a java.io.InputStream because your file will be part of the JAR created by activator dist.
My example is using it in a view but you can modify it as you want.
Play.resourceAsStream("json/Foo.json") map { inputStream =>
Ok(views.html.xxx(XXX.do_something_with_stream(inputStream)))
} getOrElse (InternalServerError)
You can also use Play.resource(), this will give you a java.net.URL, you can use getFile() to get the java.io.File out of it.
Play.resource("json/Foo.json") map { fileURL =>
Ok(views.html.xxx(XXX.do_something_with_file(fileURL.getFile())))
} getOrElse (InternalServerError)

Eclipse Plugin: Obtaining an IFile from String

I am doing some serialization which also contains an IFile path that needs to be stored as string.
I am using this IFile in a plugin project. For debugging or running Eclipse starts a new workspace. This testing-workspace has its root somewhere relatively to the plugin folder. My problem is, when I turn my IFile to an absolute path, my Eclipse testing-workspace considers the file as outside the workspace and throws exceptions.
If I use the project relative path, the IFile creation from string fails and IFile is null.
I truly want to believe that it works the way I need it, but I really would like to see it. Is there a way to reconstruct a valid IFile from a project relative path?
Currently, I am doing the reconstruction from String->IFile like this:
//name is a string with the absolute path
IPath location = new Path(name);
IFile file = project.getFile(location.lastSegment());
But, like already mentioned, works only with an absolute path, which doesn't work in the eclipse testing-workspace.
Thanks for a hint
You are very close. You want:
IPath path = new Path(name);
IFile file = project.getFile(path);
The name should be an absolute (to the workspace) or project-relative path. This interface is defined in IContainer.getFile(IPath). I changed the variable from "location" because location is usually meant as the actual (local) OS file path. To get the path use:
IPath path = file.getProjectRelativePath();

how to give the default path in gwt

i m creating a project in gwt (point) is project name ...
i want to store a image in point/war/images folder, whaich i gave the full path like C:\Documents and Settings\computer\workspace\m\war\images\ tthe the iamge will stored in images folder but i want to give the default path
i give "../war/images/" as default path, but this give me error (he system cannot find the path specified )
can any body help to give the default path
Usually you put images in the war/images directory, and you access them like
Image img = new Image("images/my_image.jpg")
You may also read this thread, and this question.

eclipse: Find Resource with a locationURI

How can i find a Resource with a locationURI?
Path path = new Path('/home/foo/eclipse/runtime-EclipseApplication/someproj/B.txt');
IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
I know there is a way by using the locationURI from current project to trim the "needless" part, but is there a "better" way to slove this issue.
You can use the following construction:
ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(URIUtil.toURI(fileName));
where fileName - absolute path of File.getAbsolutePath()
and URIUtil from org.eclipse.core.filesystem
You can use a FileLocator to find the file within your bundle, if that is what you want (instead of finding a file in the runtime workspace), see Retrieve a file from within a folder in the workspace