I have an applet I've built using NetBeans, called AKApplet. It runs fine in the IDE, but when I put it in a web page it throws the following error:
Exception in thread "Thread-15" java.lang.NoClassDefFoundError: AKApplet$2
at AKApplet.run(AKApplet.java:675)
The applet uses the run() method to load some data in the background while keeping the UI responsive. Pretty standard stuff. At line 675, after the data has been loaded, I'm trying to update the UI components using invokeLater():
public void run() {
// ... data loads ...
// line 675:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
userMessages.setText("Data loaded.");
panelList.setVisible(true);
validate();
}
});
}
The components I'm trying to update are userMessages, a JLabel and panelList which is a Panel. I don't think it's getting that far however.
Does anyone know what might be happening? At this point the applet has loaded and the components can be seen and have been updated, etc.
Make sure you're deploying not only AKApplet.class, but also AKApplet$1.class, AKApplet$2.class, etc.
I guess I don't understand what the $ classes refer to. There is only a single AKApplet class, no inner classes. There are no static definitions either.
I do have two other classes defined, but they are separate classes:
class ThreadFlags { /*...*/ }
class DeleteButton extends JLabel { /*...*/ }
Also, I've verified that they are in AKApplet.jar file at the root level:
META-INF/MANIFEST.MF
META-INF/AKAPPLET.SF
META-INF/AKAPPLET.DSA
META-INF/
AKApplet.class
DeleteButton.class
ThreadFlags.class
Update: Ok, I found the AKApplet$.class files in the /build/classes/ directory of the NetBeans project. I added them, and it works. Thanks for your help. Can someone give me a brief explanantion of what those files are? As I said, there are no inner classes that I've defined...
Are there any static definitions in the second inner class of AKApplet that could throw any kind of exception?
Exceptions in the static initializer are the most common cause for NoClassDefFoundErrors after you have made sure that the class file exists and is on the classpath.
Related
package achieveStrength;
import javax.swing.*;
public class SalutonFrame extends JFrame {
public SalutonFrame () {
super ("Salutation Mondo!");
setLookAndFeel();
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void setLookAndFeel () {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
}
public static void main (String[] args) {
SalutonFrame frame = new SalutonFrame();
}
}
In Eclipse I am receiving an error that is in the title of this question. I don't understand why this error is being thrown. I also do not understand how a class can be accessed if it is not on the same page of code in a program. I have some website development understanding so I may be able to create an analogy for someone who has that understanding as well. Here it is:
My guess is that if the main method accesses a class that is not within the file that the main method is written in then the two files that are working together must be held inside of the same folder like image files are held inside of a website's folder along with the .html file that requests the use of that particular image file. I hope this makes sense to someone out there. I really want to understand Java in order to use it well.
Thanks in advance for any help I may receive!
You're defining an achieveStrength.SalutonFrame class, it should be defined in a SalutonFrame.java file inside an achieveStrength folder which parent folder should be defined as a source folder.
For example, this is the hierarchy I would expect to find :
projectName/ # project root
src/ # source folder
achieveStrength/ # package achieveStrength
SalutonFrame.java # class achieveStrength.SalutonFrame
Newbie at netbeans-platform.
How can I save my state from one execution to the next.
The netbeans platform elegantly remembers the state and position of all my windows. How can I add to that state some of my own data? Very much like Netbeans saves what projects are open and reopens them when it starts up, along with their state.
Ass suggested here I added the following to my TopComponent but it doesn't work. getPersistenceType is called but neither writeExternal n'or readExternal are called.
#Override
public int getPersistenceType() {
return TopComponent.PERSISTENCE_ALWAYS;
}
#Override
public void writeExternal(ObjectOutput oo) throws IOException {
super.writeExternal(oo);
}
#Override
public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException {
super.readExternal(oi);
}
Comments here suggest tapping into readProperties and writeProperties but that doesn't feel right to me. I am not wanting to store Properties, I want to store State.
Some years ago I blogged about this, using the Session Storage feature of the Swing Application Framework in a NetBeans Platform application:
http://puces-blog.blogspot.ch/2009/04/netbeans-platform-meets-swing.html
The following 3 classes should provide the integration into the NetBeans Platform:
ModuleApplicationContext.java
ModuleLocalStorage.java
Modules.java
The referenced XProperties and JXTable you only need if you want support for SwingX classes such as JXTable.
To use this feature in your own module you need to initialize the context in your ModuleInstall class:
public class Installer extends ModuleInstall {
private static ModuleApplicationContext applicationContext;
#Override
public void restored() {
applicationContext = new ModuleApplicationContext(Modules.getModuleInfo(
Installer.class));
}
public static ModuleApplicationContext getApplicationContext() {
return applicationContext;
}
}
For a given contentPane you can then store the GUI session state using:
Installer.getApplicationContext().getSessionStorage().save(
getContentPanel(), SESSION_STORAGE_XML);
and restore the state using:
Installer.getApplicationContext().getSessionStorage().
restore(getContentPanel(), SESSION_STORAGE_XML);
Note: you need to set the component names of the relevant components
You can find the complete sample here: http://sourceforge.net/p/puces-samples/code/HEAD/tree/tags/sessionstate-1.0/
Also note however that development of the Swing Application Framework (JSR-296) has been withdrawn.
There is a fork called Better Swing Application Framework, but I haven't used it yet.
I also had some problems with this but finally I could fix it.
Annotate Your topcomponent class with #TopComponent.Description and set the right persistence type inside the annotation.
Your topcomponent class has to be serializable so,
every fields inside the topcompent have to be serializable or transient.
You can implement Your custom serialization with readExtern/writeExternal but it is not necessary, You can remove them.
If it still does not work check the log after You closed Your netbeans app and You will see why the platform could not serialize Your topComponent.
Background: I've created a JavaFX application, embedded in a Swing frame using JFXPanel. I've been using Eclipse as an IDE. The "Main application" is another class which only serves to create an instance of a class which extends JFXPanel to load my .fxml file when it is instantiated. When executing the main class from Eclipse, all is well, the fx:controller specified in my .fxml file has its initialize() method called (I can tell from changes it makes to the UI on load) and there are no problems.
However, when I package everything into a JAR and try to add my JFXPanel extension class to a Swing JFrame instance, it manages to load the .fxml file just fine-read images, style sheets, etc, and the rest of the code is functioning as expected however the fx:controller's initialize() method is never called. I have no problem accessing the class from other classes inside or outside the jar and I've even tried setting up a ControllerFactory that will return an instance of the Controller as well as trying all sorts of combinations of setting the FXMLLoader's classloader and using both the static and non-static methods of invoking load(). The result is always the same: it will work when launching from the IDE but does not when launching from my packaged jar. I know the jar isn't missing any files because like I said there is no issue finding the class from the Java code and the bundled fxml/css files seem to be loading fine, minus the controller issue.
Anybody ever encounter this before or have any idea what might be going on with the FXMLLoader failing to set the Controller? Could this be a bug of some sort?
I had a similar problem when packaging my JavaFX software into a .jar file. Turned out it was a problem regarding relative path. You're IDE has no issues with this, but then when compiled within a jar it is having issues.
This was resolved using following code to call my .fxml file.
getClass().getClassloader().getResource("/my/view/selector.fxml")
To say that this is the "reason" for your bug, I'm not sure, but this sure stumped me for a while and seems to be pretty much the problem I had.
Original question : Executable Jar limited to one window with JavaFX
I was unable to solve this problem. While the fxml/css files are loading fine and referencing the right controller class, I was still unable to see the initialize() method of the controller class get invoked once everything was packaged up into a jar.
Since the only thing I needed the controller for was to grab the various UI objects defined in the fxml file so that I could do real programming with them, I opted instead to just create a recursive search to look for these individual widgets by their fxml ID [seems to look up 'id' first then 'fx:id' if an 'id' isn't found] in the Scene tree..
//grabs fxml file relative to root of the jar
FXMLLoader loader = new FXMLLoader(ClassLoader.getSystemClassLoader().getResource("app.fxml"));
Parent javaFXRoot = (Parent) loader.load();
public Node findWidgetByID(String id, Parent javaFXRoot)
{
return findObject(root, id);
}
private Node findObject(Parent root, String id)
{
for (Node node : root.getChildrenUnmodifiable())
{
if (node.getId() != null && node.getId().equals(id))
{
return node; // found the node, return it
}
Node retValue = null;
if (node instanceof Parent)
{
retValue = findObject(((Parent) node), id); // recursive search
}
if (retValue != null) //if our node was found by the recursive search, return that
{
return retValue;
}
}
return null;
}
I had the same problem where the initialize() method was called from the IDE, but not from a (shaded) jar.
The problem was that we used ProGuard which was configured to keep protected and public methods. However, the initialize() method was declared private. Therefore it obfuscated the method name, JavaFX couldn't find any appropriately named method and initialize() was never called.
To stop ProGuard from obfuscating your JavaFX annotated methods and fields, include this rule into your proguard.conf:
-keepattributes javafx.fxml.FXML
-keepclassmembers class * {
#javafx.fxml.FXML *;
}
The first line will keep the #FXML annotations, the other rule keeps #FXML annotated class member names.
I am trying to open a URL with the default Windows browser, in Java. Unfortunately, I cannot use the Desktop class utilities since the code has to be compatible with 1.5.
As a solution, I am calling ShellExecute by using a native method:
public class ShellExec {
public native int execute(String document);
{
System.loadLibrary("HSWShellExec");
}
public static void main(String args[]) throws IOException {
new ShellExec().execute("http://www.google.com/");
}
}
I put the DLL file in the Eclipse project root which apparently is included in java.library.path .
Everything works just perfect if ShellExec is in the default package, but if I move it in any other package, the native call fails with:
Exception in thread "main" java.lang.UnsatisfiedLinkError: apackage.ShellExec.execute(Ljava/lang/String;)I
at apackage.ShellExec.execute(Native Method)
at apackage.ShellExec.main(ShellExec.java:13)
Anybody has any ideea why? I am using the DLL from http://www.heimetli.ch/shellexec.html
Thanks
..later edit:
Eventually this class, and others, will be utility classes in an Eclipse RCP application, and all the external DLLs will be placed in a common lib folder to which the java.library.path will point to. The DLLs are seen, but I get the same type of errors as the simple example from above.
pass the VM argument -Djava.library.path=<path-to-dll-folder> to your project launch configuration.
The block you are loading the library in is not static to the class, just defined as an anonymous block in an instance of ShellExec. Since you never create an instance of ShellExec, the anonymous block never gets called and the library never gets loaded.
Instead you should have
static {
System.loadLibrary("HSWShellExec");
}
I think that will solve your problem.
EDIT: Sorry, I just started programming in Java. It turned out to be a problem with an out of range array access... I am used to error messages about this kind of thing being automatic...
(using Netbeans 7.0.1)
I have been customizing JTextArea and JTable. I do so by adding a new Java class to my project and then declaring it extends the particular class I want (in my case, either JTextArea or JTable).
I had been using it normally, adding these new classes to JDialogs and JInternalFrames without any problem. I do so by just dragging it to my JDialog or JInternalFrame...
But recently, for some reason, I started getting this error messages "Component cannot be instantiated. Please make sure it is a JavaBeans component."
The JInternalFrames that were accepting the old customized classes still accepts them. But if I try to add the new customized class, it gives me that error message and, afterwards, it starts showing the same message to the old customized classes too...
Something really weird is going on. I copied the same code of a (previously) customized class to a new class (changing the name of the class, of course). Then I try to add this to my JInternalFrame. It gives me the error message! If, before this, I try to add the same customized class (with the original name), it adds the class normally....
This is annoying and I can't solve it. Can anyone help me please?
Thanks a lot for this answer but, if you want to know the reason here you are.
Typically this appears on two position:
an overridden method on your component.
a normal method on your component.
For example:
package UI.Components;
public class LabelComponent extends javax.swing.JLabel {
private javax.swing.JLabel label;
public TextFieldComponent() {
label = new javax.swing.JLabel(_label);
add(label);
}
#Override
public void setText(String text) {
label.setText(text);
}
}
The method setText(String text) is called say in the supper class constructor then it the overridden new method would be called in the case of the (label) variable which is used on this method still no being initialized so a java.lang.NullPointerException will be thowed.
solution:
1) try ... catch:
#Override
public void setText(String text) {
try {
label.setText(text);
} catch (Exception e) {
}
}
2) check:
use null initialization on declaration
private javax.swing.JLabel label = null;
then check on the method
#Override
public void setText(String text) {
if(label != null)
label.setText(text);
}
3)use initialization on declaration:
private javax.swing.JLabel label = label = new javax.swing.JLabel();
and then use setText method in your constructor
label.setText(_label);
note:
in the case of reason (2) a normal method on your component, it is the same as (1) but you may call the method before initialize the variable or assign null to the variable before calling the method and so on and it can being solved by the same ways.
I too faced the same problem, after some search in the web I found the solution for this problem. I don't have a deep understanding of why and how this problem occurs, but I can share with you the solution I found.
When you get such error msg, goto the menu View-->IDE Log or you can open the log from windows_user_Home\.netbeans\7.0\var\log
In that log you have to locate the error msg you got, for example,
INFO [org.netbeans.modules.form.BeanSupport]: Cannot create default instance of: test.Application1
java.lang.NullPointerException
at test.Application1.initLabel(Application1.java:906)
So the problem is in line 906 of your .java file. Open that file and comment those lines and then you will able to overcome the problem.
You can add the component to the Form or jInternalFrame or ...
After adding the component, you can again uncomment those lines. Just Clean and Build your project.
Hope this helps..
Goodluck
reachSDK
I have encountered the similar problem, however in different context.
I have two separate projects, a swing built user interface, and another one that poses as class library.
I added a class to the class library, headed over to the user interface, and implemented this newly added class from the library into the swing interface project in shape of an existing custom JFrame. So what happened to me now that the class loader of course could not find the class because the library project required compiling. The issue was fixed by compiling it.