Typing this from my phone as I got off my computer and am about to go to sleep so please ignore typos. Anyway I have a project I was working on that makes a jar file that allows me to config some things and then build a new jar file with what I set. I decided I did not like the class names for the builder and the output file, so I changed them (just changed com.john to com.epix type of thing) and updated all required things. The builder can be built and ran perfectly fine, and the output file can be built from eclipse and ran perfectly fine, however now when I try to build the file using the builder it tells me "main class not found" when I try to run the file. The thing that is really bugging me is if I pack the jar file my self (putting the nessesary files into a zip then renaming it to a jar) it works, which is where I get confused as its packing the same exact files. I assume there to be something wrong in the builder itself but the only thing that changed code-wise were just some imports. Has anyone else encountered this type of problem or know a fix? And yes I set the main class in the manifest and class path. Can't paste any code right now, as previously stated am on phone. Any ideas would be greatly appreciated, I have been scratching my head for the past 4 hours.
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.
I have a java project: simple telegram bot. It worked while I didn't tried deploy it on Heroku. I did a backup for stable version, but Eclipse keeps showing me this ERROR. IDK what's wrong with it, I tried a lot of stuff, like what's the problem? I have src folder, classes also, I have main... Maybe problem in .gitignore file?
But I deleted from it *.class, it's still not working. Help
It's not a duplicate, because I'm workin in Eclipse and it worked earlier. Added my folder properties. I need short answer, not just a wiki of possible problems because it doesnt help.
Folder props
Screenshot error and folders
Your project configuration is very bizarre; you have class files in a directory named 'SRC'. It's hard to tell what you did to manage to create a project def this bizarre. Some ideas:
You've compiled this on the command line. You should probably look into build systems like maven or gradle to build your code. Even if not, use the -d switch on javac to ensure the class files end up in a directory named 'bin' or 'build'.
You've turned off eclipse's autocompile function. Turn it back on.
Right click your Point.java file (not Point.class) and pick 'run...' from the context menu. Assuming autocompile is on this will work.
Generally, use packages. Stuff in the default package is unrunnable in various scenarios. From what I can tell you're not in one of those 'default package is not runnable' scenarios but perhaps I'm missing something.
I have scoured this website for the answer, but none of the solutions I have found worked for me. Please mark as duplicate and paste link if you know that post is the answer.
Anyway, what is happening is I am using Eclipse for my Java project. My professor emailed us classes that he coded and wants us to use for part of the project. I have not been able to figure out for the life of me how eclipse imports these .class files into the project. I have tried several solutions from other posts like linking the folder with the .class files, and none of that worked.
The reason I know it does not work is because I am supposed to be able to do "extends MessageState" etc. (MessageState is one of the .class files he sent us), and yet I get an error that Eclipse doesn't know what that is referencing (because it thinks it does not exist). The classes he sent are all super classes of my subclasses that I have to code myself in the project.
Let me know any ideas you have and I will try them out.
"EDIT"
The link provided was the first thing I tried, and it does not work. I still am getting an error in my class that extends one of the classes he provided to us. I need to use what is in his superclasses (.class, not .java) for my subclasses (.java) that I am currently writing. Here is what it looks like:
public class ServerBeginState extends MessageState
The error is on the "MessageState" which eclipse says: "MessageState cannot be resolved to a type"
This would mean that MessageState doesn't exist and needs to be created, but for some reason I am not linking the classes to this project correctly. That is what I am asking the question for.
So i just decompiled the .class files and made them into new .java files. That seemed to have worked, but if someone finds a way to use .class files for implementation in eclipse (because i know it works in Textpad 8) please post an answer.
Simply Create a new java project.
Then drag and drop the classes sent by your Professor in the folder named source code or "src" .
Eclipse will produce a pop up window with 2 options make sure you select "copy files"
if that does not work try manually copying the files in your src code location which will be found in your java project folder.
I'm trying to export a project of mine containing four classes to a jar file (not executable) using the eclipse export method. everything seems to go fine, all four classes show up in the referenced libraries once I have added them to another program.
However, when importing the classes back into a program, I can only see three of them. One of them (the same one each time) does not show up to be imported. Any suggestions as to what might be causing this or how to fix it?
I have tried cleaning and rebuilding the project before I export it, but this doesn't help.
Thanks!
EDIT: Solution has been added to answers, apologies but I discovered the issue.
I apologise everybody but i discovered the solution. The class that was giving me issues had lost it's public modifier at somepoint, probably during refactoring. Once i'd added it back, the jar file behaved properly. Sorry about that.