Main class not found while launching app

Hello, I need help for a project. I can't launch my application .jar in the browser with the extension CheerPJ. It says : "cant find or load main class". It seems that all my files are correctly configured. Can you help me ?
44 Replies
Panini
Panini2mo ago
Hey, what extension are you using? Our extensions are made for Applets and Java WebStart applications. Most of the time when you see an error like "cant find or load main class" it means CheerpJ can't find the needed files from your HTML file. Is it possible to see the HTML to verify if everything is set up correctly?
Artworks
ArtworksOP2mo ago
Artworks
ArtworksOP2mo ago
I have downloaded CheerPJ for JNLP and I try to launch a .jnlp file from HTML file. Here is my HTML file : <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>CheerPJ</title> <script src="https://cjrtnc.leaningtech.com/4.2/loader.js"></script> </head> <body> <script type="module"> (async function () { await cheerpjInit({version:8, status:"default"}); cheerpjCreateDisplay(1200, 600, document.body); const exitCode = await cheerpjRunJar( "Blackjack_card_game.jar"); console.log(Program exited with code ${exitCode}); })(); </script> <a href="http://localhost/dashboard/xelfi/xelfi.jnlp">Lancer l'application Java</a> </body> </html>
Panini
Panini2mo ago
Are you using the CheerpJ JNLPRunner extension? When using the extension there is no need to add CheerpJ to the HTML file, the extension handles all that for you. You will just need to click on the <a> tag to download the .jnlp file and the extension should trigger. You can remove this tag <script src="https://cjrtnc.leaningtech.com/4.2/loader.js"></script> and also the <script> tag in the body. You can read more about the extension in our docs here: https://labs.leaningtech.com/docs/cheerpj-jnlp-runner/getting-started/using-first-time
Artworks
ArtworksOP2mo ago
I'm using the right extension. I will remove those lines and try again. But do I have to receive a license key ? I get one keystore, is that the same kind ?
Panini
Panini2mo ago
A license is only needed for commercial use, for personal use you won't need one.
Artworks
ArtworksOP2mo ago
Yes. I have still the error
Panini
Panini2mo ago
Did the extension page trigger when downloading the jnlp file? And you get the error when pressing "Start Application"?
No description
Artworks
ArtworksOP2mo ago
Yes
Panini
Panini2mo ago
It might be that the codebase is not correctly defines in the .jnlp file. Could you also share the jnlp file if possible?
Artworks
ArtworksOP2mo ago
I try to launch from localhost and my config files and .jar, .html, .java files are all in a repertory coming from a server Apache, htdocs. My codebase is : localhost:80/dashboard/../xelfi I use as well Open Web Start, not Java Web Start. <?xml version="1.0" encoding="utf-8"?> <!-- JNLP Example --> <jnlp spec="1.0+" codebase="http://localhost/dashboard/xelfi" href="xelfi.jnlp"> <information> <title>Jeu de carte du Blackjack</title> <vendor>Cédric De Craim</vendor> <homepage href="http://localhost/dashboard/xelfi"/> <description kind="short">application en Java Swing du célèbre jeu de carte</description> <icon href="http://localhost/dashboard/xelfi/Blackjack.jpg"/> <icon kind="jeu de carte blackjack" href="http://localhost/dashboard/xelfi/Blackjack.jpg"/> <offline-allowed/> </information> <security> <all-permissions/> </security> <resources> <j2se version="1.8+" href="http://java.sun.com/products/autodl/j2se"/> <jar href="Blackjack_card_game.jar"/> <jar href="lib/JWSAntTasks/org-netbeans-modules-javawebstart-anttasks.jar"/> <property name="key" value="overwritten"/> </resources> <application-desc main-class="blackjack"/> </jnlp>
Panini
Panini2mo ago
The JNLP file looks okay at first glance. Could you share the network file when testing the application with me to make sure all the JARs get download correctly? And also the browser console output when testing the application with CheerpJ? We have documentation on how to save both of these files here: https://cheerpj.com/docs/guides/cheerpj-debug#extracting-a-har-file https://cheerpj.com/docs/guides/cheerpj-debug#saving-the-browser-console-output Just make sure to open the developer tools before pressing "start application".
Artworks
ArtworksOP2mo ago
Ok. Java Web Start works only with Java 8 or earlier. Is it true ? And it requires signature for Java applications
Panini
Panini2mo ago
Web Start is only supported by Java 8 and earlier yes. The signature is only needed when running the JAR files locally. If you want to run your application with CheerpJ you can also test it without the extension. We have instructions here: https://cheerpj.com/docs/getting-started/JNLP
Artworks
ArtworksOP2mo ago
I don't know how to use CheerPJ without the extension Actually I'm using Apache server with XAMPP, I will change it. It is easier to work with http server I will download JRE too because I'm using JDK 8
Panini
Panini2mo ago
Thank you for uploading the files, but the HAR file is unfortunatly empty. Could you please try to save it again? Open the developer tools before clicking "Start Application", then press the "Start Application" button, wait for the error to happen and the save the HAR file from the network tab. Don't close the developer tools in between. It's likely that the extension is unable to download some the JAR files or that some dependencies are missing.
Artworks
ArtworksOP2mo ago
I will do it and give you the files Actually the extension is downloading the JAR and I can see that happening on my screen but an error 403 occurs, connexion refused Before It was downloading a jnlp file instead for me to open. But that's ok because I have used the wrong extension, applet runner instead of JNLP runner. Both extensions can't be launched at the same time.
Artworks
ArtworksOP2mo ago
Artworks
ArtworksOP2mo ago
Hello, I send you the HAR file
Panini
Panini2mo ago
The extension is unable to download the Blackjack_card_game file. Can you make sure the file is available under http://localhost:8080/xelfi/Blackjack_card_game for you? I in the original JNLP file you shared the file is referenced as Blackjack_card_game.jar but the extension just tries to fetch Blackjack_card_game.
Artworks
ArtworksOP2mo ago
Yes it is available because when I get connected on the server with that url you mentioned, I retrieve my file amongst other files. In the JNLP file I have just a short path to the JAR file, it is at the root of the project.
Panini
Panini2mo ago
So if you copy this URL into your browser it downloads a file? http://localhost:8080/xelfi/Blackjack_card_game The HAR file shows a network error is happening and it's unable to downlaod the file and that is why the application is not starting.
Artworks
ArtworksOP2mo ago
It does. When using the port 8080 I have to make sure CORS is enabled. It is but while downloading I get error showing "request failed" above the extension. And next the console shows there is an issue with favicon.ico In the console network I see that there is error response Maybe some dependencies are missing. Hello, the extension downloads the jar but doesn't start the application because of the main class not loaded or found. The class is in a repository named src and it is in the file manifest.mf. I don't know why the extension doesn't find it.
Panini
Panini2mo ago
The errors on the console log and the network error in the HAR file all point to the extension not being able to download the needed files. The HAR you shared did not show that any jar files got downloaded. The JNLP file you shared also does not point to a .jar file, it only says <jar href="Blackjack_card_game" main="false"/>. There must be something wrong with how the resources are exposed in the jnlp file.
Artworks
ArtworksOP2mo ago
I have modified these lines. Now it is working. The error is coming from my file path class.
Panini
Panini2mo ago
Can you please share the updated HAR file after you modified the jnlp file?
Artworks
ArtworksOP2mo ago
I will do it. I'm trying to compile my project in other way. I want to make sure that all the packages and the paths for the classes are correct. Do I need to add prefix to paths ?
Panini
Panini2mo ago
Depends on your directory strucure. We can help once we have the new HAR file.
Artworks
ArtworksOP2mo ago
Panini
Panini2mo ago
Thank you for the new file. As you said downloading the files now seems to work fine. Make sure you are defining the main class correctly in the JNLP file. If you define the main class like this classes.blackjack in your JNLP file the pathes in your JAR file need to look like this:
└── cardgame.jar
└── classes/
└── blackjack.class
└── cardgame.jar
└── classes/
└── blackjack.class
If the structure in your jar file looks like this:
└── cardgame.jar
└── blackjack.class
└── cardgame.jar
└── blackjack.class
You can just set the main class to this blackjack
Artworks
ArtworksOP2mo ago
Thanks. I will try this. Hello, my project is called cardgame, inside of it there is a package named "src" and inside of it a new package named "classes". My main file .java and my class files are there. But I have to give the full qualified name(FQN). After I copy this in the JNLP file. So my class file is well named classes.blackjack
Panini
Panini2mo ago
Make sure that this structure also reflects in your cardgame.jar file. You can verify that using this java command from the terminaljar tf path/to/cardcame.jar. You should see an output like this:
META-INF/MANIFEST.MF
classes/blackjack.class
...
META-INF/MANIFEST.MF
classes/blackjack.class
...
Artworks
ArtworksOP2mo ago
I can try this. The jar file works when I launch it. To compile my project I have used javac -d "filename".java. Next I have used this command : jar cfv cardgame.jar blackjack.class manifest.mf -C src/classes . That creates and builds the jar file perfectly.
Panini
Panini2mo ago
Would it maybe be possible to deploy the example using github pages so we can properly test it by running the example locally?
Artworks
ArtworksOP2mo ago
Yes, can you explain how to deploy ?
Panini
Panini2mo ago
GitHub Docs
Creating a GitHub Pages site - GitHub Docs
You can create a GitHub Pages site in a new or existing repository.
Artworks
ArtworksOP5w ago
Ok 😉 Hello, I have published github page site. Url is : https://cedec1985.github.io/index.html you have to click on the link below.
Panini
Panini5w ago
Thank you for deploying the example on github pages. First off, the cardgame.jar file is compiled using Java 17 which is not supported by the JNLPRunner extension. The JNLPRunner extension is meant to run with applications that used to be deployed with Java Web Start, which was deprecated in Java 9. You already initialise and try to run the application on the landing page, so there is no need anymore to use a JNLP file like you did on your page here: <a href="https://cedec1985.github.io/xelfi.jnlp">Lancer</a> Secondly, I am unable to run the cardame.jar file in the browser with CheerpJ and locally with Java 17. Again, there is something wrong with how you deploy/package your JAR file. I tried multiple times:
java -jar Downloads/cardgame.jar
no main manifest attribute, in Downloads/cardgame.jar
java -jar Downloads/cardgame.jar
no main manifest attribute, in Downloads/cardgame.jar
java -cp Downloads/cardgame.jar classes.BlackjackUI
Error: Could not find or load main class classes.BlackjackUI
Caused by: java.lang.ClassNotFoundException: classes.BlackjackUI
java -cp Downloads/cardgame.jar classes.BlackjackUI
Error: Could not find or load main class classes.BlackjackUI
Caused by: java.lang.ClassNotFoundException: classes.BlackjackUI
java -cp Downloads/cardgame.jar BlackjackUI
Error: Could not find or load main class BlackjackUI
Caused by: java.lang.NoClassDefFoundError: BlackjackUI (wrong name: classes/BlackjackUI)
java -cp Downloads/cardgame.jar BlackjackUI
Error: Could not find or load main class BlackjackUI
Caused by: java.lang.NoClassDefFoundError: BlackjackUI (wrong name: classes/BlackjackUI)
When listing the file contents of the JAR file I get this output:
jar tf Downloads/cardgame.jar
META-INF/
META-INF/MANIFEST.MF
BlackjackUI.class
BlackjackUI$1.class
BlackjackUI$2.class
Card.class
Deck.class
Icons.class
jar tf Downloads/cardgame.jar
META-INF/
META-INF/MANIFEST.MF
BlackjackUI.class
BlackjackUI$1.class
BlackjackUI$2.class
Card.class
Deck.class
Icons.class
There is no /classes subfolder in your JAR and everything is just available from the root. So if you have an import/package statement at the top of your java files that says this: import classes.BlackjackUI but there is no classes folder in your JAR file you won’t be able to run the JAR. You need to make sure that the JAR has the correct structure.
Artworks
ArtworksOP5w ago
Hello, I have all built with Java8 first because that was the only suitable version. But the error was still there while launching. There was some issue with the classpath. I don't understand the last point. In fact, when I try to build my application all the files go in the classes folder. And the console says that the MANIFEST.mf is well added by using the command. So I will check out if there is a issue with the classpath by modifying the project settings.
Panini
Panini5w ago
It makes sense that the compiled class files are in a folder called classes. But you need to make sure the JAR file will also contain that folder. When you use jar cf, it preserves whatever relative paths you pass to it. You need make sure you call the jar command from the parent directory and also pass the /classes directory to the jar command. Something like this jar cf cardgame.jar classes/.
Artworks
ArtworksOP5w ago
That seems correct. Hello, I have tried your solution. It works too. The class files appear in the jar file. However, if I do as you tell me it says that src is missing from the root project. But I need it because the pattern is already made like this. I should change the pattern or start project from the beginning.
Panini
Panini4w ago
Hello again, I am sadly not familiar with your project structure so I can't help you with compiling your application. If you are able to recompile your Jar to also include the class folder or change the package it should also work with CheerpJ.

Did you find this page helpful?