How to save/serialize information in executable .jar java files - java

I recently made a simple Gui Library program, which can make new books and store them on shelves. When I ran the application from eclipse, it serialized the files and When I reopened the application, I was able to load old information off of it.
However, when I converted it into an .app and ran it, it functioned normally, but I was unable to save (serialize) data, and consequently not able to retrieve data when I opened it up again.
I tried searching my computer for that .ser file, but it seemed to not have been created.
I assume this has something to do with the JVM, but I'm not so familiar with the ins and outs of the JVM. It also may not. Do you know why my program wouldn't serialize properly and how I could fix it
Thanks
M.R.

Related

How to avoid providing path in the code in Java swing

I have an Exe file created using Java Swing. I recently added a below code to open multiple session of an application at the same time. It works fine.
Runtime.getRuntime().exec("C:\Auto\format.exe")
The application is used by different users and hence they are forced to save the application on the same path. If not, they would be able to open multiple sessions.
How do I avoid using path here on this code or Is there a better option for me?
Thanks in Advance.
You could get rid of the path, when your *.exe file is in the same folder or subfolder of your Java program.
The only thing you need to care about is, that of your program is installed on somes computer, that you take the *.exe with you. You just could copy the files or you could create an installer (my suggestion).

How do you send NetBeans Java Project with Database to .exe?

[I know this question has been asked here and here, but I don't quite understand the answers, and I didn't want to ask questions on old threads.]
Problem:
I have a project that I created in NetBeans (7.01), and I created a database to go with it using Java DB. I would like to have a .exe file that I can run on any computer, so that when someone clicks it, the program runs like it should (establish the connection with the db, run the rest of the Java code).
What I know:
After a little reading, I see that if I didn't have the DB, the executable that I am looking for would really just be the .jar file. I also found that I needed to create an embedded db which in part means that I need to change the URL from jdbc:derby://localhost... to something different (but I'm not sure what something should be. Just a path to the source code files perhaps with the db information?).
Question:
I have a bunch of pieces (not sure if they're all right), but I am not sure how to put them all together. Could someone let me know how to create an embedded DB and send that, and the rest of the program, to an .exe?
Thanks in advance!

Java save method stops working when .jar is moved

I’m coding my first java Desktop application using eclipse and I’m having difficulty deploying it. My project uses JavaFX2 and the e(fx)clipse plugin, the latter is in charge of generating the build.xml file.
ABOUT THE APP
The app, amongst other things, provides an interface where the user can create categories and associate these with labels. All modifications are saved within a single file (the data is stored as a serialized object.) and are supposed to be loaded automatically when the app is reopened.
THE PROBLEM
When build as an executable jar using a .xml file (Ant), the project runs fine within the folder where it gets created. I can run my application, modify data, and save everything once I’m done. When I reopen the app everything gets loaded as it should.
However, if I copy the folder elsewhere, I can no longer save any data. Everything else seems to work; the app will even load the data that was saved when it was in its original directory. I assume that this means that the app can still see the data file, but can no longer write to it.
WHAT IVE TRIED
I’ve read that warping the .jar around an installer may fix the issue; however, one of the goals for the app was to make it as portable as possible. Meaning that it should be possible to move it around from one directory/computer to another, ideally in a manner that is cross-platform friendly, without the need of installing it.
I’ve tried various things to get it to work. I’ve shifted the whole project to Netbeans (to produce a different build), I’ve modified the save/load method file path to make sure the right document is targeted, I’ve tweaked the .xml file the best I could, and I even tried to build the project using the javafxpackager. No matter what I do, when the build works, I get the same results.
Right now, I’m thinking that there may still be something wrong with the .xml file but I’ve got a hard time understanding how to modify it. Perhaps the problem is somehow caused by the way the data gets serialized. I know that at one point when I moved things around within my project, both the save and load methods could no longer interact with the data.
What I find strange is that when the project is moved the load method still works. If the problem is caused by changing the file path, how come only the save method ceases to function?
APP FILES AND STRUCTURE
+src
-(Main.java)
+controller
-(misc.javas)
+modelData
-(Library.java) -->the object that is serialized
+modelLogic
-(misc.javas)
+view
-(misc.javas)
+files
-(library.data) -->the file where the serialized object is saved
+lib
-(empty.empty)
So, is their anything I can do to solve my problem?
Thanks in advance.
In the end, the problem was rather simple. When I was moving the app, I was always putting it on the desktop, which, in the case of my PC, sets by default all content as read-only (in relation to the app's privileges). Because of this, the app could not modify any files that were within folders on the desktop.
Therefore, all I had to do was to move the app to another directory, such as C:\randomFolder, and the problem was solved.
So, if anyone has a similar problem, moving the app elsewhere may be the solution. Alternatively, taking full ownership of the folder and its content can also work.
When possible, it is usually better to have the app ask its user for a specific location to save its data.

Can jar files be decompiled to get original source?

I have a major problem that happened to me. I am writing a program that has taken me 3 months+ to make and today I accidentally deleted the source code.
I am working in NetBeans and I had just cleaned and built it, so I still have the working .jar file. The program was saved on a flash drive, not my computer, so the classes are not in the recycling bin.
Can anyone tell me about how I can go about getting my source code back?
Unless you explicitly packaged your source code in the JAR file, there's no way to get back the original source. You could try using a decompiler (like this) but that will hardly give you nice, readable code.
The best solution would be to stop using your computer right now. Then use a recovery tool to recover your deleted files. The more recently it was deleted, the higher the chance you'll get it back. There are lots of tools that can be used to do this (just Google it).
You can use a decompiler ( see How to decompile a whole Jar file? ) but you won't be getting "original" source back.

java: how to copy the currently executing file

I have a jar. I want the jar to be able to make a copy of itself while running. I understand windows may have problems with this. How would I do this, or am I over thinking it?
Edit: To explain a bit more....
I'm writing a repackagable firmware deployment system... http://code.google.com/p/heimdall-one-click/ The idea is that a ROM developer from XDA can make his own, then pack it up in a cross-platform deployable one-click packaging nearly as easily as it is to deploy the firmware.
My program takes alot of the work out by automating the tasks... I'm trying to automate packaging of the one-click deployable packaging system.... give the developers a form to fill out which will change the header information, then they select their firmware files to be deployed. I'm trying to keep it all in one jar.
As josh says, it would be nice if you tell why do you want to do this in order to help.
Answering only what have you post, copying the jar is just copying another file. There is the issue that it might be blocked by the OS (Windows); another issue is how do you locate it in the machine and if the user running the process has the permissions needed.
Once those two issues are solved, it is just a copy operation, the OS could not care less that if the order to copy comes from the process run from the file or from another one.
EDIT to asnwer changes in the first post.
As I told before, in the end copying a file is a OS issue. If you want to copy the current jar in Windows, then the jar must not be locked by other process so it becomes an OS question rather than a Java one.
Possible workarounds:
The faster (but dirtiest) is to launch a .bat that does a sleep of a few seconds and then does the copy. Immediately after launching it, your close your java app. If you need to continue doing things in Java, after copying the file, the .bat launches the java app again (with the appropiate parameters).
A variant of the previous is slightly sleazier... launch your java app from a .bat, and the first thing that .bat does is copying your jar to the PC temp directory. Be sure to document it well so your users do not get scared!
JNI library to unlock a file. There are several programs that (Unlocker) that try to unlock files; do not know to which point it is effective or how will it affect the JVM.
I believe you can use:
File file = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
to get a reference to the path to the .JAR file.
Then you just make a copy of it:
http://download.oracle.com/javase/tutorial/essential/io/copy.html
Your operating system might not allow this, but I think it should.

Categories