How to specify classes from another projects to an applet? - java

I have 2 projects: my main project in which the applet is, and a second project from which I use some classes. I added the second project to my main projects build path. But when I run my applet in Chrome, it gives an error: java.lang.NoClassDefFoundError: chapter13/MessagePanel. chapter13/MessagePanel is the class that I imported from the second project into my main project.
I call the applet like this(the html file is in the main project root folder):
<applet
code = "myapplets/DisplayMessageApp.class"
width = 250
height = 50>
</applet>
What can I do so that the applet can see the "chapter13/MessagePanel" class from the second project?

The simplest thing is to bundle all the required classes in a jar file and then specify this as the archive.
Like this:
http://docs.oracle.com/javase/7/docs/api/java/applet/Applet.html
André

Related

JApplet java.lang.NoClassDefFoundError: org/json/JSONException

I have this JApplet jar and tried to open it on the web.
But I keep getting this error
java.lang.NoClassDefFoundError: org/json/JSONException
I've searched the answers here and tried them, but didn't work.
Even if I already added the java-json.jar in my build path of the project.
this is my html code for running my jar file on the web
<applet archive="PDDS.jar" code="MainFrame.class" width="960" height="540">
I have two classes inside PDDS.jar, the JApplet class is the MainFrame.class, the other one is a class for connecting to the sqlite database.
Even if I already added the java-json.jar in my build path of the project.
The build path of a project has nothing to do with the runtime class path, which is set in the applet element.
So..
<applet archive="PDDS.jar" code="MainFrame.class" width="960" height="540">
Should be more like..
<applet archive="PDDS.jar java-json.jar" code="MainFrame.class" width="960" height="540">
Note also that the code attribute should best be the fully qualified name of the applet class, not the file name, so it would look like:
<applet archive="PDDS.jar java-json.jar" code="MainFrame" width="960" height="540">

JavaFX: Need help understanding project hierarchy issues

My code has gotten quite large and so I've decided to build a runnable JAR to see how it's done. I probably should've tried sooner because I'm getting 2 different errors related to the project structure, first is the "no main manifest attribute error" when trying to run the JAR from command prompt. Double-clicking the JAR does nothing (Win7). The second issue is related to the FXMLLoader explained lower down.
I followed the steps here to build the JAR, which involved moving all Maven files into the JAR directory. The compiled JAR gave me the manifest error, so I followed this which adds a Maven plugin in my pom.xml file. The error might be caused by a wrong naming convention with the line <mainClass>com.primary.Drag</mainClass> where primary is the package and Drag is my Drag.java file (class) which has the main method.
Inititally I was using the default package but read that this is not recommended for larger projects, so I put all my files into "primary". Here is my current hierarchy as shown in IntelliJ:
The problem is that ever since I created the "primary" package, I can no longer even compile the program via IntelliJ, let alone build a runnable JAR. This is due by the second error I mentioned, which is java.lang.IllegalStateException: Location is not set. on this line within primary/Drag.java:
FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("firstlaunch.fxml")); It used to work with the default package, but not anymore.
I tried replacing firstlaunch.fxml with /primary/firstlaunch.fxml and /resources/firstlaunch.fxml (with and without moving resources into primary package) but no luck.
3 Related Questions:
Is my project structure incorrect?
How do I reference the fxml file from the primary package?
Is this what I should write in Maven's mainClass tags? <mainClass>com.primary.Drag</mainClass>
Is my project structure incorrect?
Answer:
Your package name should be like com.primary.******
How do I reference the fxml file from theprimary package?
Answer:
Always make sure that you are trying to load firstlaunch .xml from the class which is located in same package where that xml is kept. Means class which you wrote the loading code and xml file should be in same package
Is this what I should write in Maven's mainClass tags?com.primary.Drag
Answer:
If you package name corrected to com.primary , your main class Drag will correctly added by maven

Error with an applet in an Executable Jar in eclipse

I want to create an executable jar in eclipse. I tried the option that said create executable jar but it didn't let me. Then I tried creating a normal jar file with a manifest like this:
Main-Class: Game.Game
Class-Path: AppletSource.jar
Here, the executable class is an applet (not JApplet) inside the package Game and a class called Game.class. It uses a package called AppletSource.jar. When I create it, it does not run. It says it failed to load the Main-Class manifest atribute. The AppletSource.jar is in there. Did I miss anything? and is the Main-Class wrote like that if it is inside a package?
PS: I added a main method to the Game class and now it gives me noclassdefounderror. It cant find the AppletSource.jar

Java 6 : building a jar whose main class package is not at root of the jar

I would like to build a Jar whose main class package is not at the root of the jar.
In the application, I've just written a Main class
com.gmail.bernabe.laurent.test.HelloWorld
I could simply make a Runnable jar from the Eclipse IDE, but I would like to go a step further
=> Having the final jar organized like this
Root
=> META-INF
=> Lib
=====> com
==========> gmail
...
Instead of :
Root
=> META-INF
==> com
======> gmail
...
Is it possible to configure the Manifest file so that the Jar runs properly ?
Thanks in advance
Why do I want to do this ?
I want to build a JOGL-based jar organized as follow
=> JOGL (subdirectories are JOGL just needed files)
=> CORE (subdirectorie is the core application package)
=> LAUNCHER (subdirectory is the custom classloader package)
=> META-INF
Yes and no.
No, because you should not do that: imagine someone supporting your code. Or even yourself, two years later, returning to the project: you saw hundreds of jars organised as defined by JarClassLoader, you worked in tens of "usual" projects... Now, well, now you see a JAR file. Will you expect it to have a tricky custom loading scheme? I doubt so.
Yes, because you can add one more main class, use it to set up a classloader, and after that load classes from any layout. You will need to keep manifest and the startup class at the "proper" places, though.
The tutorial on class loading can be found at http://java.sun.com/developer/onlineTraining/Security/Fundamentals/magercises/ClassLoader/help.html

java project problem

I have a java project. I can run it through command prompt but can not able to run through Eclipse or NetBeans. When I run it, the error come main class is not found.
What can i do ?
How are you trying to run it in Eclipse and Netbeans? Basically you need to tell them which class to execute - which class has the main method in.
In Eclipse you can just go to the relevant class and hit Alt-Shift-X, J to launch it.
A few steps for eclipse
create a new project: Menu File/New/Project...
place your java source in the src folder of your project
through the context menu (right click the project name in navigator) you define the build path, and add required libraries.
now your code should be ready to run using the green (>) button
Is your project using libraries? I had the opposite problem where I could run my program in Netbeans and not from the jar (or command line) because the libraries were in my Netbeans folder and not my "distribution" folder.
EDIT: By libraries I mean third party libraries.
When you create a project in NetBeans, it will create a default Main class for you, complete with a main(String[] args) method. To access your code, just rename your class main method, copy it (and any dependnt classes into the project and change the package names to reflect the project name) and instantiate the class containing it in the default NetBeans main method and call the renamed main method e.g. if your class is called HelloWorld and the main method was renamed "hello" the call would look like:
HelloWorld hw = new HelloWorld();
hw.hello();
Simples :)

Categories

Resources