I've seen many other answers to similar questions, however, none of the solutions I've seen will do the trick, I've tried them all but I keep getting the same error message:
"JAR creation failed. [...] Could not find main method from given
launch configuration."
It might have something to do with my Java app using external images with Swing, since I got some problems while trying to create the JAR before getting to the final step.
The main method is there, it works if I run it from there and it has the right format, parameters etc. just in case you're wondering.
I'd appreciate some help, since it would be really useful for me to keep track of finished releases of my software. It's a Java application with no external libraries, just some images and that's it. I don't really need anything fancy, just a runnable package that I can send to some people to test for errors.
Well, for what I understand, you just need something that will run, not necessarily a "Runnable JAR" as Eclipse calls it, and what I assume you're trying to do.
I ran into this very same issue some time ago, and although I'm not sure it is the optimal solution, I'd say it will meet your requirements and it worked perfectly for me.
First off, as you probably have done before, right click on the project folder, then click Export.
Here is where we make things differently, instead of "Runnable JAR File", just select "JAR File".
The menu here is a little bit different from that on the Runnable JAR screen. Just select the resouces you'd like to export, then the JAR file destination and click next. Adjust the settings there and when you get to the final screen, select the application entry point (in this case, your Main class).
Click finish and that should work this time.
I don't really know what causes this behaviour or why it works this way, but for me, it just did every time I couldn't manage to do it using the "Runnable JAR" option.
To run the JAR package, it should be as easy as going to the console on any computer with Java installed and type:
java -jar [pathToYourFileHere]
Good luck and let me know if it works. Hope it is what you needed!
Related
I am a beginner, so I'm sorry if the question is too basic.
I've just built a little GUI based program that repeats a certain sound (a short alarm sound right now) continuously after a given amount of seconds, resulting in a sound cycle.
The issue is that when I run the program inside the IDE, it runs perfectly. But, when I build it, the built artifact (the *.jar file) does not work well. I mean, everything works well, but it plays no sound.
I've been searching for answers in the community and I've found some instructions about how to build the artifact properly. At the moment, they were based on a Maven setup most of the times. Either way, I used those answers, but the *.jar file I'm getting has NO resources inside. So, neither the required sound file.
I've tried to put the *.wav file manually, using the project artifact options, but this didn't work either, even if I am positively getting the sound file in the built *.jar file artifact.
I've also tried to convert the program to a console one (non GUI) to see any possible exceptions, but then my program didn't play any sound at all. Not even running it from the IDE. So, it's got even worse, and this is, in fact, a different issue that I don't want to mix in here, and I hope I am not wrong on that point.
In most of the answers I've found, they were giving a lot of importance on how the resource was "fetched" inside the code. In fact, it has taught me a better way to do so. I was using quite a weird way to get the absolute path of the file... But then I used:
MyClass.class.getResource("sound.wav");
And now I'm using (with the same result):
ClassLoader.getSystemResource("sound.wav");
Both "resource getting" options had also been tried with a slash bar before the file name ("/sound.wav"), with, again, the same unproductive result as I've already exposed.
Just to make it totally clear, as I mentioned in the question title, I am working with a Gradle project in the IntelliJ IDE.
Thank you in advance for your help.
The code you are using to load the resource as stream from the classpath is not correct.
You first get the URL of the resource with URL res = ClassLoader.getSystemResource("sound.wav");, then use getPath() method to convert it to String.
It works when the resource is not in the jar. But when it's in the jar, file URL becomes invalid and the code fails with FileNotFoundException.
Instead of that you need to change the code in MediaPlayer#playSound, replace
final AudioInputStream in = AudioSystem.getAudioInputStream(fileUrl);
with the following as a quick fix:
InputStream is = getClass().getResourceAsStream("/sound.wav");
final AudioInputStream in = AudioSystem.getAudioInputStream(new BufferedInputStream(is));
This will load the resource stream directly from the jar and the file will play.
See this answer for more details.
Your app will not run with Gradle because there is no support for GUI Designer in IntelliJ IDEA Gradle builds yet.
To build the jar I had to create an artifact manually and include the sound file there.
I realize this may seem like a completely stupid question and this question is a "wall of text", but I'm feeling like I've gotten way out of my depth here and need some guidance. I've read several SO questions on this topic, but still can't seem to find one that addresses this particular situation. (please reference update(s) at the end of this post)
BACKGROUND
Our company has an application that was built in Java and released as an executable JAR package by a developer who passed away a couple of years ago. Since then, there has been minimal need for even looking at that code, let alone making any changes. That's been really good because I do my programming in VB.NET (Visual Studio) and, while I can read and make sense of Java code, I'm not proficient in actually writing/debugging that code.
However, a recent issue has come up that forced me to try to take a look at this Java code due to some internal changes in organization and data structure. I've looked in his "src\" directory and found older versions of his original code but wasn't able to find the most recent version anywhere. I found some code that made it possible for me to extract the JAR that's currently being used to a local directory on CodeProject (JarsVB), so I've been able to look over some of the .java files when trying to figure out what query is being used for some random operation. I don't know what tool(s) the original developer used to create the project/JAR, so I've installed the IntelliJ IDEA Community Edition 2018 as an IDE, and it's worked for me so far to simply view the code so I can understand a bit about what it's doing.
PROBLEM/GOALS
Unfortunately, now there is a need for me to make a change to the Java code and update the JAR, and this is where I can't seem to make heads or tails of what I need to do. I have my local copy of the code from the "unzipped" JAR containing all the .java and .class files (along with all the other resources), but I'm not sure how to go from that to modifying the code and recompiling the executable JAR.
So, my goals here are as follows:
(properly) Decompile the existing executable JAR. (If the JarsVB solution I mentioned above did what it was supposed to do, I should already have this part handled, but if there's a better, more "integrated" way of doing it, I'd be open to that as well.)
Modify one or more .java files. (I believe I can figure out the syntax well enough to get this part done.)
Debug/test my changes.
Recompile the changes into an updated executable JAR for release. (THIS is where I'm experiencing the most confusion.)
WHAT I'VE DONE
I've been looking at the documentation for IntelliJ to try to figure out how to make this all happen, but I feel like I'm missing stuff. I set my "Project Structure" settings to point to a specific folder, but I can't seem to get it to actually build anything in my specified path.
I went into one of the .java files and made/saved a small change to test, then went to the Build menu and tried all the building options available to me: "Build Project", "Build Module", and "Rebuild Project". Each time, the event log shows that "All files are up-to-date" (even though I changed one), so I go to my output directory to see what it built, but the folder is empty.
I looked at the IntelliJ documentation for Packaging a Module into a JAR File, which says to
On the main menu, choose Build | Build Artifact
But that option is disabled in my Build menu, and when I look in the Project Structure dialog, the Artifacts section is empty, so I try to add a new setting (Add -> JAR -> From modules with dependencies...), but I'm not sure how to properly set that up either.
I tried to select a Main Class from my classes/ directory, but it wouldn't actually accept any of the .class files I selected, so I just left it blank. Then, I did a search for a MANIFEST file, but couldn't find one anywhere so I left that blank as well. The Build menu option is enabled now, but when I tried to Build Artifact, again, I get nothing in my output directory. In fact, the only files I can find that have changed are in my local working directory. I'm just dumbfounded at this point.
FINAL THOUGHTS/QUESTIONS
I've tried to provide as much detail here as I could think of about all the things I've tried and gone through to get this JAR updated, but if there's a question about anything, please let me know. I'm not looking for a "handout" and I don't expect anyone to do this for me, but I'm also not wanting to become a Java developer just for the sake of making some minor changes to an application that will eventually be replaced by a .NET application. I simply am not familiar enough with the tools or Java development in general to know how to get to where I want to be from where I am.
My decompiled source files are in a totally separate directory from the original, production JAR file because, when I recompile this time, I want to completely recreate the JAR. Am I understanding the Java development process correctly in editing one of the .java files I got from decompiling with the JarsVB and then recompiling the JAR?
Thanks in advance for any assistance in finding the resources I need to accomplish these goals.
EDIT/UPDATE
So, looking at the link in the accepted answer in another SO question, How to build jars from IntelliJ properly?, I figured out at least one part of my issue: Leaving the Main Class setting of my Artifacts configuration blank was a problem. Since I hadn't been able to select a .class file and I wasn't sure how to correctly populate that field, I hadn't given the IDE enough information to operate correctly. I entered the namespace and class (namespace.class) I found in the class file that apparently defines the main method for the application, then set the path for the MANIFISET.MF file to my output directory and tried again to Build Artifact.
This time, at least, it DID produce a JAR in my defined output directory, but it's only 1KB in size. As stated above, the source files are in a completely separate directory from the original JAR file from which they were decompiled. The output directory, too, is completely separate from the location of the original JAR file. Do I need a copy of the original JAR file in the output path for recompiling to work correctly?
I'm making progress, but I'm sure I'm overlooking something "stupid" at this point that's primarily due to my unfamiliarity with the IDE and developing Java applications in general.
UPDATE 2
Looking at another SO question - how to create jar of java package in intelliJ idea - I learned that I have to individually add the necessary files for repackaging into the JAR. However, this brings up the question, what files do I add? The linked question identifies the .class files, but when I go look at the .class files in my working directory, none of those have been updated recently, so it looks like I'm still missing a step somewhere.
Another question this brings up is, are there certain conventions for Java development of which I need to be aware when preparing my environment? I have my output path set to a completely separate folder than any working or production code, so I'm wondering if something in that setup might potentially cause issues.
As I said before, I made a small change to one of the .java files, then tried both the Build Module and Rebuild Project options, but those are still telling me that "All files are up-to-date". Even so, I tried adding just the .class files from under my classes\ directory to my Artifact configuration and tried again to Build Artifact. I got a bit larger file (approx. 5MB), but when I try to execute the JAR, it just doesn't appear to do anything, let alone actually launch the application.
I tried again by adding the root folder of my local copy of the source adding everything the root folder contains. (yes, the directory probably needs some "spring cleaning", but that's for another day)
This time, I got a much larger file this time (approx. 21MB), so I thought I might have fixed the problem. Unfortunately, no such luck. The JAR still doesn't appear to execute.
For reference, the original JAR file from which the code was decompiled is approx. 59MB in size so, either IntelliJ is doing an incredible job with compression, or there's yet another step I haven't yet found. I'm sure this is all a matter of getting my IDE configured correctly, but I just can't seem to find the right combination of settings.
Sorry for the crappy title.
I've been building games in Java since February, teaching myself as I go along. Recently, I worked on a dungeon crawler for over a month using Java FX, and I was quite pleased with the result. I was, in fact, so pleased with the game that I decided to publish it on my Game Jolt account, which I had previously used to produce games I made in Visual Basic.
I went through the process of building a .jar file as an Artifact in Intellij IDEA, and eventually, one was produced. When I tried to run the file however, it started to open but then crashed. I couldn't figure out why even after rummaging through my Console looking for errors to no avail (I'm using a Mac,) and after attempting to open the jar on a PC I found around the house, I concluded it was an error with the app. I soon found that I hadn't signed the jar, causing a security error, so I went through the process of trying to self-sign. When I tried to do that, it worked, but the jar still wouldn't open.
Evidently, there is a underlying issue with my app that needs to be remedied. I have no idea where to start looking, largely because I have tried several times to create new files, including a Hello World application, none of which resulted in a functioning application.
So, here's the entire project.
If any of you would like to have a look through it and point me in the right direction, I would appreciate it, and credit you on my Game Jolt page. This has been giving me headaches for far too long, and I'm about to give up and learn a new bloody language. Please help, it's always appreciated.
Sincerely, a 15-year-old Java-lover who just wants to produce a damn dungeon crawler, fgs.
UPDATE: As per #AndreyAkhmetov's advice, I removed the Class-Path from my manifest file. Unfortunately however, I still don't get anything opening when trying to launch the application. Hope this helps, everyone. Thanks for the feedback so far!
I have made a pull request to fix your game ;).
your problem is how to load resources. You can use maven to organize your project and resources. It helps to make your project platform independent and easy to share.
you should use this structure:
Crawler /
src /
main/
java/
resources/
Images/
pom.xml
The pom.xml is a configuration file and all your resources like Images are in the resources folder. Then you can load it using this:
InputStream loadimg = Main.class
.getClassLoader()
.getResourceAsStream("Images/chest.png");
chest.setImage(new Image(loadimg));
also you should change the directories where you load saved files, from this c\\... to this ./saved.txt. Then the jar can be executed from anywhere.
To create an artifact you can do
$ mvn package
or if you are using Intellij, in the side bar click on Maven then on lifecycle and package.
That will create a target folder with the jar inside, you can execute it by clicking on it or with this command to see the console errors.
java -jar Crawler.jar
Try to split your components from your Main class, it is too large and difficult to read and understand.
Nice game, good luck!
Want to warn you, that I have been tinkering around the site for some minutes and didn't find any suitable answers.
So, my question is -
What certainly, step by step, should I do from having the code done in Eclipse to make an executable program even on other PC's?
I'm using Eclipse, JRE7.
I have tried that before, but that executable .jar worked just on my PC.
I heard about keeping the .jar with lib files, but have no more ideas about that. Your help would help a lot.
You have my anticipated thanks.
I have 4 class-files in my project located in the "src" folder.
answer 1. if you are new to eclipse, then i guess your having trouble exporting a simple program that has no dependencies. to export a program follow these steps:
right click on your main project folder. click export
under java, choose jar file, not executable jar file.
on the first page you need to specify an output file. do so.
the next thing (i think) is asking you to make a javadoc or something. skip it
the next is the important one, so listen carefully. the first thing it asks is for you to make a meta inf file. you can or you can not. it will save sdome time later if you do make one.
the step will also ask you for your main class file. click browse and you should be prompted with some of your class files. the class you choose should be the class with your main public static void main(String[] args){} method. this method is critical so if you do not have it for some reason, you need to learn more about it.
click export and you should be done!
answer 2. your have the exported file but you need some dependencies like lwjgl for example. you are a more advanced coder and not new if this is your problem. it it is, go download jarmatey and learn how to use it. there is other programs that do this too but i prefer this one.
I've downloaded the full AOSP source for my device, and was trying to import it inside eclipse to make some changes to the framework (basically it includes an alertDialog that shows when a button in the system bar is pressed and i want to edit it).
I've followed the guide on the AOSP Documentation, and also imported in my build path the android-common_intermediates/javalib.jar but i still get some errors, to be specific I get errors with some fields that should have been defined in one R.java file and I have no idea of how to find the proper R file, i even doubt it's there, maybe is it generated in a second moment or is it in another path and then my compiler script moves it to the right path?
Presuming you heed the warnings in the comments above and still want an answer to the question, I may be able to help. I frequently see similar problems, when I use eclipse to view Android source. There are several projects that are, apparently, not built in response to "make" but that are still included by the .classpath file. My solution is to make them by hand.
Once you've sourced build/envsetup.sh, the command "mm" will build the project in the directory you are in. I start eclipse, find the projects whose resource definitions are missing, cd into them one by one, and use "mm" to build them.
I believe that you will find that they are all projects in the "packages" directory.
So far, this has always worked, for me.