If you want to read the images inside jar, you need to read like this: ImageIcon icon = new ImageIcon(getClass().getClassLoader().getResource("image.jpg"));
@@CodeJava Ty so much for the help! You really saved my project :D Having some difficulty trying to do the same things with my audio files tho. Is it supposed to work in the same manner?
@@CodeJava You couldn't know how long I have been search for this solution. Your one line of code finally solves my problem once and for all. Thank you.
I have a test automation project which use maven command line for executing test cases. Actually there is no main method in the whole project. How can I create an executable jar file and give to others to run the test case without source code?
Its possible. Maybe you are running your test cases with some file(.xml) that you need to write in java, where main method will be there. After that you can convert your project to executable jar and while executing java will execute that main method.
i am able to create jar for non-rest api code but for rest api code i am facing issue its saying that java 17 not avaialble in my pc i have java 19 and then i also try to change pom.xml with java 17 but still other error is throwing
I get an error when I run my executable jar file. It says "A JNI Error has occurred..." My program works when I run it in intelliJ. Any idea what the issue might be?
@@CodeJava idk lol. Turns out, when I installed Java it did not install the javac portion. So I had to reinstall Java so I would have both the jre and the jdk. Now everything works. Thanks!
Hi , if i make a basic helloworld and I build it like You show here it doesn't work, its because there's no GUI right? Or should it anyways work in command prompt like making basic helloworld in codeblocks with c++?
I think that jar file may contain the whole JRE which is needed to execute Java application. Play around with the export options to not include runtime dependencies.
I think you need to put the config.properties into the JAR file, and when reading the file, you need to get from the class resource stream, not regular file. Somethink like this: InputStream is = getClass().getClassLoader().getResourceAsStream("config.properties");
@@CodeJava thanks for the response, yes it's working but I need to put the Main-Class manually in the manifest file META-INF/MANIFEST.MF in the jar file. I have already set up the main class as you do in the video and I can see the Main-Class is set up in MANIFEST.MF in Intellij, but when the jar is built the Main-Class has gone. Did I miss something?
I have a project that requires a login and then takes you to another screen when you input good credentials. However. when I run this method, even when correct login credentials are provided, the only screen that ever shows is the initial screen. How can I create a JAR that has my entire project, not just the first screen? Any advice?
I added a jar file to a project successfully, but now i want to make some changes on the Jar file itself and I want that change to be reflected in the new project. How can i do this ?
@@CodeJava Yes I have done that, every steps I followed, it's javafx project, the project runs fine, but when build artifact, and run it shows Error:Could not load Main class
@@oguzhantopaloglu9442 are you reading a wav file from the jar itself? So you need to read it as resource stream (keyword: java read resource as stream)
@@CodeJava I have a Swing application playing music with these codes: Clip clipMelody; File file = new File("res/piano.wav"); AudioInputStream stream = AudioSystem.getAudioInputStream(); clipMelody = AudioSystem.getClip(file); clipMelody.open(s); the folder named "res" is where I want to put my resources (.wav, .png etc files). I want the application to be just in jar. I want the .wav and every single file in the res folder to be in the jar so that I can just click the jar and run the application.
there are several reasons that you can't open a jar file: missing attribute Main-Class in manifest, jar is not executable, JVM is not compatible with JAR file...
Thanks for the instructions. But please don't find this offensive but it is very hard to understand you sometimes, I can understand you but its hard. Practice spelling even though its not your first language it will help you tremendously. For example for word "image" you say "imazis" or word "Java Archive" you say "Jaf Arh". Come on man ahahaah :D
Struggling for quite a time. Thanks for the tutorial
Glad I could help.
How do you reference you're image file in this code? I'm struggling with including my resources in the jar file whenever I try to build an artifact.
If you want to read the images inside jar, you need to read like this:
ImageIcon icon = new ImageIcon(getClass().getClassLoader().getResource("image.jpg"));
@@CodeJava Ty so much for the help! You really saved my project :D Having some difficulty trying to do the same things with my audio files tho. Is it supposed to work in the same manner?
@@oskarremvang1226 did you find a solution ? I have the same problem
@@CodeJava You couldn't know how long I have been search for this solution. Your one line of code finally solves my problem once and for all. Thank you.
I have a test automation project which use maven command line for executing test cases. Actually there is no main method in the whole project.
How can I create an executable jar file and give to others to run the test case without source code?
Its possible. Maybe you are running your test cases with some file(.xml) that you need to write in java, where main method will be there. After that you can convert your project to executable jar and while executing java will execute that main method.
Thank you very much you saved me
Happy to help :)
you sound exactly like @cake_progresS
tysmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm u helped me so much
Glad I could help!
respect bro!
thank you.
i am able to create jar for non-rest api code but for rest api code i am facing issue its saying that java 17 not avaialble in my pc i have java 19 and then i also try to change pom.xml with java 17 but still other error is throwing
I get an error when I run my executable jar file. It says "A JNI Error has occurred..." My program works when I run it in intelliJ. Any idea what the issue might be?
I have no idea. Does your program use JNI?
@@CodeJava idk lol. Turns out, when I installed Java it did not install the javac portion. So I had to reinstall Java so I would have both the jre and the jdk. Now everything works. Thanks!
good job home mate.
Thanks 👍
hi do you have a similar video for netbeans ide users by any chance?Thanks
Yes, I do have videos for Netbeans users here: th-cam.com/play/PLR2yPNIFMlL8aq4_IDCKvJiA4cdWFKudT.html
include dependencies in jar run with signed security error. then i change to use maven plugin to create the jar and worked.
thanks for that information. Happy coding :)
Will the java web project also be jar?
a java web project is packaged as WAR (Web Archive) file. Check this video: th-cam.com/video/zPVS7wCevPM/w-d-xo.html
great video
Glad you enjoyed it
Hi , if i make a basic helloworld and I build it like You show here it doesn't work, its because there's no GUI right? Or should it anyways work in command prompt like making basic helloworld in codeblocks with c++?
If i open it through cmd it works. Another question, Why does my helloworld.jar weights almost 41Mb? How could I make it lighter.
you can also make a hello word Java program with GUI, as described in this video: th-cam.com/video/LgLASQ6JtnQ/w-d-xo.html
I think that jar file may contain the whole JRE which is needed to execute Java application. Play around with the export options to not include runtime dependencies.
How to add the path to that excel file into the executable jar file? I have problem with mapping the config.properties file
I think you need to put the config.properties into the JAR file, and when reading the file, you need to get from the class resource stream, not regular file. Somethink like this:
InputStream is = getClass().getClassLoader().getResourceAsStream("config.properties");
@@CodeJava thanks for the response, yes it's working but I need to put the Main-Class manually in the manifest file META-INF/MANIFEST.MF in the jar file.
I have already set up the main class as you do in the video and I can see the Main-Class is set up in MANIFEST.MF in Intellij, but when the jar is built the Main-Class has gone. Did I miss something?
I have a project that requires a login and then takes you to another screen when you input good credentials. However. when I run this method, even when correct login credentials are provided, the only screen that ever shows is the initial screen. How can I create a JAR that has my entire project, not just the first screen? Any advice?
yes
I just lost my whole project because of you!! 😭😭😢😢😡😡
@Snowey :3 No, I found my mistake and now will recreate my project and create its jar file.
hope things went well for you.
@@CodeJava Thanks. Love from India. : )
I added a jar file to a project successfully, but now i want to make some changes on the Jar file itself and I want that change to be reflected in the new project. How can i do this ?
I think the best way is to add the jar project as a library/reference project of the current project.
howto place dependencies inside lib folder for example so structur like student.jar and lib/*.jar
modify the artifact details of the module
I get Error:Could not find or load Main class
I don't know what's wrong, please help me😭😭😭
did you specify the main class that will launch the application?
@@CodeJava Yes I have done that, every steps I followed, it's javafx project, the project runs fine, but when build artifact, and run it shows Error:Could not load Main class
Error:Could not load or find Main class
@@AkashKumar-im2zy I am having this same problem. I tried everything and nothing works.
How do you reference your sound file in this code? I have a .wav file that I can't reach
it just reads the WAV file from the file system.
@@CodeJava So what can I do instead of this:
File file = new File("res/piano.wav");
@@oguzhantopaloglu9442 are you reading a wav file from the jar itself? So you need to read it as resource stream (keyword: java read resource as stream)
@@CodeJava I have a Swing application playing music with these codes:
Clip clipMelody;
File file = new File("res/piano.wav");
AudioInputStream stream = AudioSystem.getAudioInputStream();
clipMelody = AudioSystem.getClip(file);
clipMelody.open(s);
the folder named "res" is where I want to put my resources (.wav, .png etc files). I want the application to be just in jar. I want the .wav and every single file in the res folder to be in the jar so that I can just click the jar and run the application.
cant open jar file !
there are several reasons that you can't open a jar file: missing attribute Main-Class in manifest, jar is not executable, JVM is not compatible with JAR file...
Thanks for the instructions. But please don't find this offensive but it is very hard to understand you sometimes, I can understand you but its hard. Practice spelling even though its not your first language it will help you tremendously. For example for word "image" you say "imazis" or word "Java Archive" you say "Jaf Arh". Come on man ahahaah :D
I don't feel offensive at all. You're right, I need to improve my pronunciation. Thanks for watching :)