I'm working on a project with some friends over Github for a University project. I've only just taken my friends code off the repository for the first time. For some reason, all references to images in the code don't seem to allow compilation due to a directory problem I think. I'm using Eclipse, my friend's using Netbeans(don't know if that affects it or not?). Anyway, all of the images referenced are either in a folder known as runone, on the same level as the Eclipse src, or within the package 'runone' within src. I don't know which.
Here's an example of some of the references:
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/runone/OSTeeeennny.jpg")))
jLabel53.setIcon(new javax.swing.ImageIcon(getClass().getResource("/runone/clown_fishTIny.jpg")));
I guess what I'm wanting to know is, how can I make these resources work correctly, on any machine that we code this program on?
Hope that all made sense!
For the examples you have given your file structure might look like this
src/com/yourpackage/YourClass.java
src/runone/OSTeeeennny.jpg
src/runone/clown_fishTIny.jpg
For a more best practice way of organizing your resources you could do this
src/com/yourpackage/YourClass.java
src/com/yourpackage/resources/OSTeeeennny.jpg
src/com/yourpackage/resources/clown_fishTIny.jpg
and then use the following calls to load them
getClass().getResource("resources/OSTeeeennny.jpg")
getClass().getResource("resources/clown_fishTIny.jpg")
Related
I'm making a small program that will make the user chose between about a thousand pictures (thumbnails) and then display this picture in full size. I have the pictures saved on both my computer and on a CD, so how do I go about "importing" these to the program? Is the best way to put all the pictures in one huge picture and import it? And if I wish to make the program run on somebody else's computer, what do I do then?
If you are trying to use this application on another computer i would put the pictures on some sort of cloud service that can be reached anywhere then jar up the java application and point to the file path of those images. Hope this helped :)
To ship files with an application, one can put files in a package along with sources and read a file with ClassLoader.getResourceAsStream.
If you are using maven or gradle for build/project, put the package under src/main/resources and not in the src/main/java folder.
Note on Android this may not work, put images in assets folder and use Context.getAssets().open.
If you have a huge database, I would advise to a webdatabase software (SQL like). Here is a tutorial on how to use a SQL database with java; https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html
I hope this gives you ideas on how to solve this issue.
I'm running a Processing sketch (a PApplet subclass, created by exporting from a Processing sketch) from Clojure. The basic process is
export sketch
locate jar files in exported application
install these jar files into local maven repo
load the libs in clojure using lein
The code is in this GH repo, including (reasonably) minimal instructions for how to get set up and reproduce the problem.
Loading basic sketches works, but when I create a sketch which calls e.g. loadImage() to load an image from my data folder, the sketch can't find it.
I know this is convoluted way to do this - this is partially a learning exercise for me and partially the fact that I'm a lisp guy but not a JVM guy.
Is there any way to dynamically tell the PApplet subclass where its data folder is? Or am I stuck putting absolute paths everywhere?
EDIT: Ugh, what a mess. It looks like the problem is that I'm actually wrapping each PApplet subclass in a clojure proxy (so that I can intercept certain methods and make the various sketches play nice together) but the way Processing finds the data path involves looking at the ProtectionDomain to find the root source path, so this won't work as I'm currentyly trying it. Back to the drawing board...
So you're using maven to link to jars you've exported from Processing, and those jars can't find the sketch directory?
If I were you, I would look int two places:
The Processing JavaDoc contains a detailed reference on every variable and function in Processing. Particularly check out the PApplet class and check out the following:
ARGS_SKETCH_FOLDER
selectInput()
selectOutput()
sketchFile()
sketchOutputPath() and sketchOutputStream()
sketchPath()
I would bet that by either setting the first variable or by overriding some of those functions, you can have Processing look wherever you want.
If you need more detail, the Processing source will tell you exactly how Processing uses all of the above.
Ok, Please be gentle and kind. I am taking an Android Programming class in college and its our teachers first semester so he is learning as we are... Nothing against him, he's a good teacher, just doesn't know the answer.
I don't know how to put this in Android/java language so here goes...
Is there a way to use something similar to Includes, in web design, in Android. I am looking to add similar java code from one activity to the next, can i make a file that if i change this one file it will change in all the files its included it.
I know it can be done with the XML files but i couldn't find anything to show that it can be done in the actually coding... but then again i don't know the correct terminology. Please any help would be great. And thank you for taking the time to read and answer me. I apologize if this was somewhere else, I just couldn't find it.. Thanks again
Put anything you wish to "include" in Java classes, then import them. http://leepoint.net/notes-java/language/10basics/import.html
Android Programming class! I would've loved that in University...
Regardless, if you're using Eclipse for your IDE, it should pickup any changes from your java imports automatically. If you choose to refactor other java classes within your project, a rename for example (with "Update All References"), will update all references to that class across your project.
You could also use Maven (dependency management) to pull the latest third-party (or other) .jars at build time, if that's applicable to what your needs are.
:)
I use Eclipse to write Java code and use DropBox to sync my code with others' across our multiple computers. Most of the time, everything works as expected: if anyone makes a change on either end, the change is saved and when the other person refreshes the Eclipse workspace, the changes come through and can be viewed and run successfully.
Sometimes, one of several errors arises. Sometimes Eclipse says it cannot find a main class and sometimes it says it could not find the class itself. Sometimes it will not report an error but for some reason will not actually update the .class file and therefore run an old version even though the compiler displays the new source code and that saves. I've then noticed that if I manually copy the code into a new .java file elsewhere in the file system and then compile it, it works fine, but for some reason it refuses to regenerate the .class file and I have to delete it manually and replace it with the one generated in the other project--then it works. But for solving the other problems everything needs to be manually copied, deleted, and re-pasted....
[The actual errors include NoClassDefFoundError, UnsupportedClassVersionError, and some other error related to not having a main class.]
I realize that the description here is somewhat vague, but unfortunately I'm not entirely sure what's going on. I hope I'm just missing some basic fact that would help solve all these problems.
Thanks!
I'm sure you will see issues using Dropbox for sharing your source.
Eclipse does not know what Dropbox is doing whilst it's uploading and downloading updates and their activities will certainly not be synchronised. At arbitrary points in time when Eclipse tries to do builds etc. it will find unexpected activity going on, maybe even partially downloaded source files which might explain the specific errors you are seeing.
You're trying to do something more complex than sharing photos or documents. The advice I would give is to use a source control system like git or subversion for source code sharing and control. You can then make use of plugins for Eclipse that are designed to integrate these systems in an easy to use way. There's a learning curve there, but the skills will serve you well.
You can use online versions of these solutions like github and unfuddle if you want to consume sharing, backup and version control of source as a service like you do with Dropbox. They're free, too.
Subversion, Git and all version control software solve all of these problems for you.
Dropbox is not really an adapted system for sharing code. What you should do is set up a SVN, and commit only the source files. This way, you won't have these kind of errors.
Dropbox does have versioning (you can restore old versions of a file), and doesn't seem to be a horrible solution for the problem. I keep my Eclipse repository on Dropbox so it is available on any computer; but since I only use it myself, I haven't encountered your problems.
There is one case I can absolutely see you running into problems--it's if your class files are stored in the dropbox as well. This would just screw everything up. Make sure you specify a location on your local hard-drive for all build artifacts (classes, jars, ...) and that the only thing on your dropbox is the .java sources.
In fact, I suggest you don't keep your eclipse project in your dropbox, just create your eclipse project and point it to the java files in your dropbox.
If this doesn't work for you, go with what other people here said and set up a SVN repository somewhere, it's easier than you would think.
Oh, another possible problem--dates! You may want to make sure the date on your java files isn't jumping forwards and backwards (as might happen if one of your developers were in another time zone). In this case, Eclipse may prefer not to re-compile your file.
Also, instead of the copy/etc procedure you are currently going through, try forcing a project clean.
Response to request for more info:
When you start Eclipse, select/create a workspace that is NOT on your dropbox. The best place is probably off your home directory. If you have already specified a default workspace, there should be a switch workspace item in the file menu.
Create your project. select "Create project from existing source" and specify the source files in your dropbox. I think you want "create separate folders for source and class files" to keep your class files out of your dropbox. If you see anything saying "Copy files into your workspace, say "no".
This should give you a valid, working project. I hope you don't see those problems any more.
One more thing may help--and this may work on your existing project--without the above procedure...
Whenever you refresh your files (f5) to load in changes from the dropbox, select the Project/clean menu and select the project. This should delete all the class files and rebuild them.
If your classfiles are shared on the dropbox, this could still have strange consequences on other people with eclipse open, so I really do suggest rebuilding your workspace as I said above.
How to avoid no main class
Provide one. That issue has nothing to do with DropBox
I have a very specific question.
I want to compile an ActionScript project within Java in-memory with the help of the Flex Compiler API ( http://livedocs.adobe.com/flex/3/compilerAPI_flex3.pdf ).
My last problem is to add images (jpg, png) that are embedded in the project to the compiler. I donĀ“t see a possibility to do this in-memory and my attempt to add them from disk was also unsuccessful.
Hope someone has a good hint for me.
Greets, Konsumierer
Invoking the flex compiler from java shouldn't be any different from invoking it from the eclipse plugin. I suppose you have the project located somewhere on the filesystem, so I suppose if the images are in the correct relative path, they should be embedded. How do you embed them, actually?
Another option is, in case when you use them in web-environment (and not AIR), to just write a relative server path to the image. For instance - the .swf is in /, you can poing the image to /images/myimage.jpg, and it will work.