How to correctly make app structure to use resources? - java

can anyone tell me how to make directory structure of desktop app, to proper use of resources? Here is example of my app structure (using maven). It worked until I tryied to change packages structure, I only renames folders.
New structure:
src/main/java/com/example/appname/app/App.java //main class with Application, just runs gui
src/main/java/com/example/appname/gui/GuiFrame.java //JFrame
now I have resources this way, but it doesn't work:
src/main/resources/com/example/appname/app/resources/App.properties
src/main/resources/com/example/appname/gui/resources/GuiFrame.properties
after clean and build, netbeans makes me:
target/classes/com/example/appname/app/App.class
target/classes/com/example/appname/app/resources/App.properties
target/classes/com/example/appname/gui/GuiFrame.class
target/classes/com/example/appname/gui/resources/GuiFrame.properties
But when I run it, on Swing controls I don't see any text, which is inside .properties file, they are empty.
may I set something somewhere? Thank you for answers.

Maybe after your refactoring you need to change the way you load resources, i.e. from getResourceAsStream("/App.properties") to getResourceAsStream("App.properties")?

Related

NetBeans save edited .jar

I am currently using a library for "Notify My Android". The library is using an outdated URL so i tried to change it. I attached the source file and now I can edit the code. Before attaching the source file it just said "compiled code". But when i save it it does not seem to save the changes. It is still using the old URL. Also the change I made is underlined in blue. I hope somebody knows how to make the .jar to accept my changes.
Thanks in advance
it's highly discouraged to modify jars you depend on simply because if you ever want to upgrade versions you'd need to modify the new jar you are looking for.
In those situations you have these options:
if it is an open source project, contribute to the project and correct the URL
try and set the property from your code (this may not be possible in certain situations)
try and extend the class you're trying to use and set the URL on the property you need (like the previous one, it may not be possible to do this)
this should be your last resource: create your own project (from the original jar), make the changes you require, package it up and add it to you app.

Embed an executable .jar inside a jframe

I have a .jar of a game that I would like to embed inside a JFrame of my own program. So when I run my program, it will also launch that .jar and it will be wrapped in my JFrame. I'm not sure if this is even possible, haven't found a way yet, so any guidance is great :)
I can't offer much more context because even after an hour or so of googling I'm still completely lost on where to start, if you need any other information feel free to ask and I'll get it asap.
A .jar file is not a swing widget; it's Java archive and can contain class files. What you want to do is add the jar to the classpath, and then load a JWidget class from there.
The only way I know how to do this by hacking the jar. Create a new project, add it to your class path. Look at the manifest and what class the main method is in.
Then create your own main method which calls MainClass.main(args);
Then do a sneaky Frames.getFrames() as described here Get Any/All Active JFrames in Java Application?. Once you have a handle the jframe you can 'steal' the components and add it to your own and presto!

How to build JAR files dynamically

I'm making a program that needs to be able to let Clients change a setting, and using what I'm calling a "Builder", create a .jar that replaces some constants in a class with their settings.
In other words, I have a GUI that has a few textfields so that when they press the JButton labeled Build, it creates a new Runnable Jar that in a Constants class whose settings are changed with what was in the textfields.
Is this possible? I've heard about ANT Scripts, but I'm not really sure if that's what I'm looking for here.
Thanks
have you considered using a .properties files or something similar instead? You can use ant scripts for what you are describing (check out http://ant.apache.org/manual/Tasks/replaceregexp.html, you could use this task in your build.xml to dynamically change the .java files but it seems a little kludgy) but it might not be the best solution.
Check this page: http://www.mkyong.com/java/java-properties-file-examples/ which has some detail about saving to/loading from a properties file. You could set up your constants class to load it's state variables from this file, and set up the Build JButton to create that properties file.
I'm trying to think of a use case where you would want to modify the class source itself rather than use a properties file, but to be honest I can't. So I suppose you may have some special circumstance where this is not a tenable solution for you, but 99% of the time this is how I would suggest you go about it.

Unable to import image resources into our GUI

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")

How to avoid no main class and no class def found error using DropBox and Eclipse for Java

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

Categories

Resources