Grails newbie here. My application is in Grails, my IDE is IntelliJ IDEA. I configured my project (in IntelliJ) to say that my resources folder is under root\src\resources. I have an image file in there that I need to load into an InputStream / BufferedImage. However I can't seem to get to those resources from my Grails controller. This is what I was trying:
def image = this.getClass().getClassLoader().getResourceAsStream("image.png")
But that just returns null. I use this exact same convention in Java projects (well, except I have to declare image being of type InputStream), and it does work there. I was under the impression that I could essentially drop Java code in a Grails project and it should just work. What do I need to do differently to get this to work in my Grails controller? I need to access that static resource file.
If you mark a directory as a source directory in IntelliJ IDEA, Grails won't know about it. You have to configure Grails properly by either adding your new directory as a source directory or move the resource to one of the standard source directories.
I've never actually added a new source directory myself, but the answer to this stackoverflow question looks promising.
Other than that, you can just add resources to any source directory and it will be included, for example: grails-app/conf, src/java, src/groovy and more. In addition, any file in web-app/META-INF/classes will also be in the classpath of the application. The last one is great to know about if you need to copy a java or groovy source file (i.e. just copy, no compilation).
Try this
servletContext.getResourceAsStream("/WEB-INF/resources/yourfile")
Related
I have a java source file in my project, that I want to move to an entirely different location than the rest of the files (my unix home dir), but I still want to be able to properly use it in my project. Is there a way to do this?
EDIT - I see there is a vote to close this for being unclear, so - let's say you have a pJava project in Eclispe. All the source files are neatly saved in their relevant packages, under the same directory. Now, I want to move one of the files to somewhere completely different, but still have it used in my project. I hope this clarifies
Thanks!
I suggest that it depends on what your reason is for moving the source file.
The Java file is still conceptually part of this project, but you're moving it for general organisational purposes. In which case, the new "completely different" directory is another place where sources should be read from, which most IDEs will call a "source root". You should configure your project to read sources from there as appropriate for your IDE.
You're moving the file because it's distinct from this project. In which case I would suggest it ought to be a separate project in its own right. In order to still use the logic in your original project, you'd build the new project into a JAR, and then bring in the JAR as a library dependency (either directly, or using some dependency management system such as Ivy/Maven/etc.). Again, the details will depend on what your current setup is.
Right click on your Eclipse's project -> properties -> java build path , and under the source tab click Link source then choose the parent folder of your java file .
I have a rather large project, which uses a lot of jars, all meant for normal usage and therefore use files as the following:
new File(relativePath);
Now I need to use that project as part of a Jersey/Maven/Glassfish project, which surely enough has a much better way to load file, namely as an InputStream.
Now, some of the files in my original project are only compiled classes which means I can't change the way they load the files (I can change the file's path though).
I've packed the entire project using the 'pack for store' and gave it a try, but all I got was a FileNotFoundException.
Is there any way to pack the project along with all the resources to run as a black box, or as if it was running locally?
Any other ideas on how to do that would also be greatly appreciated.
I have to use a Javascript file in my GWT Project. This Javascript is in a common library project and I deploy it together with my GWT Project using ANT.
So, I have no problem in production environment: but I cannot test it in development phase.
I tried to create an Eclipse link to Javascript resource but seem that GWT "can't see it".
Some behavior with other kind of resources (images, css etc.).
Is it a bug or is there another way to do?
I'm using Eclipse Juno, GWT 2.5.0 and Debian 7.0.
Thank you.
Can you just use script tag in your project's .html file?
If you're using some kind of source control, it usually has a way to make a link to a dependent project or file. Simply include that link in your GWT Project, and reference the file through there.
If you can't or don't want to do it through your source control, do it through your OS. Since you're using Linux, simply make a symbolic link to the common file/folder using ln -s (if you were using Windows, you'd need to run mklink from the command line), and reference the file that way.
In either case - source control or OS - you'll be able to see the file(s) when you refresh your project in Eclipse, and modifying one will modify the other in its own directory.
Edit - information on symbolic links in CVS
I haven't played with CVS in quite some time, so can't speak much about its capabilities for symbolic links. A bit of googling said it's not supported, though there are workarounds. One workaround is to add script files that run during checkout. That sounds like it may still be tough to make OS-agnostic. I did find one site that mentioned using module aliases to get the same result. Maybe that will give what you need. An excerpt from the site follows:
One common way to handle situations like this in CVS is to alias the
collection in a modules file rule. -Checkout the "CVSROOT" module and
you'll find the "modules" file; simply change it and check it in like
anything else, with the exception that when you check in CVSROOT files
they "activate" at the same time. The example below may look a little
kludgy, and it is because AFAIK you can't redefine a directory and alias
it at the same time, sadly. I'll use a typical Java situation as its
package system lends itself well to this kind of thing:
Real module directories are "a", "b", and "common"
Directory alias for all common srouce
_common_src_all -d src/com/mycompany/common common/src/com/mycompany/common
Full "A" project including common
a_all &a &_common_src_all
Full "B" project including common
b_all &b &_common_src_all
I have this Java code some other person wrote, specifically JSPs. I am trying to understand where everything is.
In my index.jsp (the main file which is loaded) it imports a certain namespace (I suppose tomcat does all the compiling, I don't know):
<%# page import="org.sgrp.SearchResults"%>
This physical location doesn't exist in my CLASSPATH so I suppose it's referring to a namespace inside the .jar code structure (correct me if I'm wrong).
So how am I suppose to find the source code for this? Does Tomcat set a specific CLASSPATH location for each project?
EDIT
I'm trying to understand if Tomcat follows a certain structure so I can find where the source code for this stuff is.
From the perspective of a web
application, class or resource loading
looks in the following repositories,
in this order:
* Bootstrap classes of your JVM
* System class loader classes
* /WEB-INF/classes of your web application
* /WEB-INF/lib/*.jar of your web application
* $CATALINA_HOME/lib
* $CATALINA_HOME/lib/*.jar
from the Tomcat docs.
The most likely locations for classes specific to your application are the WEB-INF/lib/*.jar and WEB-INF/classes. However as others have mentioned, this will contain compiled class files and you wont be able to see the exact java source (even after decompiling)
jars are compressed javabyte code. In order to get the sources you'll have to decompile them. There are a few utilities out there, but working with decompiled code is a bit of a nightmare if you ask me, I wouldn't recommend it, especially not if you didn't already know this. :D
The source code will generally be stored elsewhere.
As far as your specific questions on Tomcat, I don't know, having never working with Tomcat before.
You can look for the jars in two places:
/web-inf/lib directory (it has all the custom jars in use by the app)
The build file (mvn, ant etc)
Source code might not be there at all. So, first of all, you need to find which jar exactly has that imported class. Then just search the web to find the project/API website. There you can look for the source, if its an open source library. Otherwise, decomplilation is the only option left, and that might not be very helpful.
class path is, in my experience, commonly set in the ant build file of each project
I was unfortunately forced to result to uploading a WAR file as my backup for a web application I am working on.
Luckily I have the most recent WAR file available. I am using Eclipse IDE and am using the Web Tools plugin for all the J2EE work that I am doing with the Dynamic Web Application Project.
When I imported my WAR file, and ran it on a local server, everything works fine. The problem I a ran into is that in the Java Resources/src folder that all my packages and .java files were now only consists of all the same packages, but they are empty.
I checked to see if I could find the files and I found the .class files in an "Imported files" folder that is not accessible in the Eclipse Project Explorer. I believe that I need to do some type of build or something so that my .java files are available for me, but unfortunately this is one area where I lack.
One thing I would also like to know is, one way or the other, am I able to obtain the .java source code files if I have access to the .class files?
Also, I would like to configure this environment as it was before where my Java Resources:src folder contaiend the packages and .java files.
One thing I would also like to know is, one way or the other, am I able to obtain the .java source code files if I have access to the .class files?
The short answer is No. There is no way to regenerate original source files from bytecode files.
If you were really, really desperate you could try to use a Java bytecode decompiler on your bytecode files, but the result will be be nothing like your original source code.
All comments and javadocs will be gone.
All original code layout will be gone.
Original local variable and parameter names may be gone, depending on your original compiler switches.
Constant expressions may have been pre-evaluated, and loops, string concatenations and other constructs may have been transformed unrecognizably.
Depending on the maturity of the decompiler, the Java code might not be semantically equivalent to the original code, and might not even be compilable.
I hope you haven't spent too long developing this application because the best answer may be to start again.