A javafx program within another javafx Program - java

I would like to know what is the right approach to building a program within another program. I've finished building Project A(calendar application) with javafx. It looks up events for any days that have events saved to them. I want to embed this program as a feature inside another javafx program. I've been looking up modules and not sure if thats the right approach to accessing the specific files i need to use. I also want an fxml file in project B.(Library Management System), to be able to <"include"> another fxml file by accessing it from project A. Is this possible and if so am I supposed
to copy all the files from A and put them in B or do more research on using modules to accomplish this?

I've fixed it by exporting the old project as a jar and putting it in my other project which accessed the files I needed it to access. I hope this is a good approach but it works in the end at least.

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 create a Java application that auto-updates itself?

It is as simple as an application that can update itself.
More specific, maybe a launcher that loads a jar file from the web and replaces it with the current jar.
The approach that I want is:
A launcher which is coded in Java.
That launcher overall uses classloader e.t.c.
I want to keep the data from the previous jar. Maybe the data from the previous jar are written on a .txt file and pass them to the new one? (If possible)
I have no clue about classloader or any direction to follow.
So my question is "How can i use classloader to create an auto-update Java application?".
P.S. I want java web start to be the last choice. Some lines of code would help me very much.
Edit: I understand completely what are you saying in theory. What i am looking for is specific links or code which i can take as reference.
The question isn't a duplicate because i can't find links or code but theory.
You can use the URLClassLoader to load a jar file from an URL. Mind, URLs may also be local files, and I'd recommend that you download the jar file locally before loading it in, instead of loading it directly from the web. The reason for this is that you want the application to remain usable, even if the user cannot reach your server.
However, I'd also recommend against taking this approach. What if you want to update the launcher? It's better to download a seperate updater application from the main application, launch the seperate updater application, have that kill the main application and update it.

Create and Run a simple java Program in intelliJ without all the advanced features?

I am just learning java and have come to a point where coding in 'vi' is killing my hands
I'm considering an IDE like 'intelliJ' with code Completion but don't want to use all that project tree , packages etc and be caught up in its complexity, i'll learn how to use an IDE later ,
Can someone tell me how to just create and run , just a .java file along with codee completiona and debuggin , so that i dont need to save with 'wq' , compile and open vi.filename.java each time, this is why i want an IDE , to make this easier, so please guide me on how to do just this in IntelliJ or suggest some other IDE with good codeCompletion,
I know this question is subjective but i;m askinng it since i just had a hard time typing a simple GUI quiz Application
I would just create new Java project (File/New Project/Java), select Create project from template and choose Command line app. This creates a project containing a class with empty main method, which you can run by hitting SHIFT + F10 or debug using SHIFT + F9.
This whole process takes only like 20 seconds.
And as far as the directory structure complexity goes, you have just one directory that you need to be concerned about - src/. You can keep all your classes in the default package if you want and therefore all your code will be in src/ without any subdirectories for packages.
I don't think it gets any easier than this.
Most IDEs make you create a project to start coding, so I usually have one project called 'test' for whenever I want to try a bit of code. Make one java class file that has a main class and use F5 or find the run option in the menu.

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.

How to run another Java project from a running project specially in NetBeans and in case of a desktop application?

I have some database applications (CRUD), and I want to create a desktop application with some buttons such that clicking on them lead to run those applications. How could I do this?
I have tried adding the CRUD projects as class path for the desktop application, but it didn't work out, with an exception in CRUD's lines indicating a class can not be found or something.
I'm using NetBeans and those database applications were created automatically by NetBeans from my MySQL server.
OK, I found a solution on this although this is not what I wanted, but it's working.
First, I built all the database applications as JAR files.
Next, in the event handler of my buttons I added this line of code in order to execute the JAR file:
Runtime.getRuntime().exec("java.exe -jar CRUDapp1.jar");
So every time I click the button the application runs.
Although this is working fine for me, but it seems not to be the right solution. I rather prefer solutions like adding classpaths or similar, but because of some conflict in the class name, that I don't know where they come from, classpaths didn't work.

Categories