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!
Related
I am a beginner Java developer and I have created an application in Java (Netbeans).
I have used buttons icons, backgrouds for jframes etc. When I built the project, they can easily accessible using WinRAR. Anyone can extract my jar file and see all the images that I have used in my program. Even the images used in jpanel that is not accessible without password.
How can I hide or protect these images?
I have created a .exe file also using Launch4j but still facing same problem. When I right click on the .exe file, its easy to extract my whole program.
To be honest, if someone wants your picture, they are going to be able to get it. One can simply do a print screen. However you can still do somethings.
Remove the extensions for the picture files and give them crazy names - this will make it harder for people to find your pictures
Encript the files then have the application decript them when loading - I don't know how to do this but shouldn't be too hard to find, for instance you could save the pictures as a byte stream with some alterations then reload it.
Save the picture in some archive which only your application knows how to read.
But anyway even with all these things, I still know how one could get a handle to an open JFrame, look through the components, and then could get your picture. IMHO trying to prevent people for getting your pictures is not worth doing.
I have a jar program and Im trying to use JWrapper to create an installer for windows and for mac. This is the first time I try this so forgive me for ths basic question, but I cant figure out how to fill in the different fields in the JWrapper interface. For example, Im asked to provide the classpath. I tried pointing to the folder which contains the jars which my program uses but that doesnt work. I am also asked to provide the path to "Files". What do they mean by that? I would appreciate any help. Thanks.
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.
Short and sweet: I'd like to be able to run another .java file, from the click of a button in a GUI in Netbeans for Mac.
A little more explanation:
I am recreating a project I created when I started with java, to make it cleaner. I have done a lot of improvement, though one problem I had with it is that I had too many panels in one JFrame, and I ended up having large amounts of code setting visibilities whenever I change panel.
Eg:
jPanel1.setVisible(false);
jPanel2.setVisible(false);
jPanel3.setVisible(true);
I know I could just live with it, but I'd like to see if there is a way to separate the code/panels a bit more, as described in the following.
My first thought was to have multiple files; one for each page. Similar to how you would in html.
Therefore, I have LoginPage.java, and GuestMainPage.java.
If it is possible, I'd like to be able to link them, like: close LoginPage.java and open GuestMainPage.java, on the click of a button.
Basically, I want to be able to make it compile or open another .java file, when I click a button on my GUI.
(In case it helps, the file would be in the same project.)
Example If there is a way to open another file. Possibly by accessing terminal commands?
public class LoginPage extends javax.swing.JFrame {
//Excluded a bunch of generated code from Netbeans
private void Login(java.awt.event.MouseEvent evt) {
//Code executed on Mouse Click of a Button labeled Login.
if(username.equalsIgnoreCase("guest")){
//A bit of pseudocode
open(GuestMainPage.java);
close(LoginPage.java);
}
if(username.equalsIgnoreCase("admin")){
open(AdminMainPage.java);
close(LoginPage.java);
}
}
}
From research and experience, I know you can access other files, or other classes within the same project - thus the use of objects and such - but I don't know if you can physically run the other file. I'd be able to live with the possibility of being able to access terminal through java. (And then have the power to do basically anything, I just cannot find how to do that either.)
I have looked through a lot of stackoverflow topics to try and find an answer, as well as a lot of googling, but I cannot find one that really answers my question. However, if there really is no way to do this, then I am perfectly happy with an answer confirming that, and if that is the case, I will just have to cope with multiple panels.
Thank you
-Ewan
No, you don't "open" other files like that, nor do you want to. Instead you compile all Java files to class files and use other classes. You can load classes on command using a custom class-loader, but I sincerely doubt that your application really needs that. Instead it just needs some MVC (model-view-controller) structuring (as noted by ilea -- 1+ to them) and forethought.
One decent solution to consider is to use a CardLayout, since this can greatly simplify changing GUI "views" or components shown in a top-level container such as an applet or a JFrame. You can read up on how to use these guys here: How to use CardLayout
You should try implementing an MVC pattern. Take a look at spring: http://www.springsource.org/
As I see that you are going to do as same as me. Please start this from the little. Let's suppose that I have a ".java" file and I want to run it with the JFRAME button, so can I create this ".java" file into a JAR file and then with the JBUTTON I can run that JAR file with this code.
try {
Desktop.getDesktop().open(new File("Path of the *.jar File"));
} catch(IOException ex) {
System.out.println(ex.getMessage());
}
Try this.
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")?