I am trying to set up GLFW, but so far, I am getting stuck on the very first step, which is calling glfwInit(). My code is below:
import static org.lwjgl.glfw.GLFW.*;
public class Main {
public static void main(String[] args)
{
if(!glfwInit())
{
throw new IllegalStateException("Failed to initialize GLFW");
}
}
}
I am getting this error: Exception in thread "main" java.lang.ExceptionInInitializerError at org.lwjgl.glfw.GLFW.glfwInit(GLFW.java:1046) at Main.main(Main.java:6) Caused by: java.lang.IllegalStateException: GLFW may only be used on the main thread and that thread must be the first thread in the process. Please run the JVM with -XstartOnFirstThread. This check may be disabled with Configuration.GLFW_CHECK_THREAD0. at org.lwjgl.glfw.EventLoop.<clinit>(EventLoop.java:30).
For reference, I am on a Mac, and am using eclipse, and have every single LWJGL jar file included in the project classpath.
Will running JVM with -XstartOnFirstThread like the warning says fix the issue? And if so, how do I do it?
Related
I am setting up a program for a class I am taking. All the code has been provided and I have configured everything into Eclipse. I also had to use becker.jar as an external jar file in the Libraries -> Classpath. When I run the program an error occurs about a NullPointerException.
I already tried changing the becker.jar into the modulepath instead of classpath but then Eclipse cannot find the file. I also tried reinstalling becker.jar as well as redoing the entire setup for the project.
Here is the code I am trying to set up:
import becker.robots.*;
/*
Starting Template:
This file was created in order to provide you with a pre made
'starter' program
*/
public class Starting_Template extends Object {
public static void main(String[] args) {
City toronto = new City();
Robot jo = new Robot(toronto, 3, 0, Direction.EAST, 0);
new Thing(toronto, 3, 2);
jo.move();
jo.turnLeft();
}
}
When running this error shows up:
Exception in thread "main" java.lang.NullPointerException
at java.desktop/sun.font.FontDesignMetrics.getDefaultFrc(FontDesignMetrics.java:158)
at java.desktop/sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:279)
at java.desktop/sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1183)
at java.desktop/javax.swing.JComponent.getFontMetrics(JComponent.java:1646)
at java.desktop/javax.swing.plaf.basic.BasicLabelUI.getPreferredSize(BasicLabelUI.java:245)
at java.desktop/javax.swing.JComponent.getPreferredSize(JComponent.java:1680)
at java.desktop/javax.swing.JSlider.updateLabelUIs(JSlider.java:853)
at java.desktop/javax.swing.JSlider.setLabelTable(JSlider.java:824)
at becker.robots.x.<init>(SourceFile:32)
at becker.robots.RobotUIComponents.<init>(SourceFile:87)
at becker.robots.RobotUIComponents.<init>(SourceFile:110)
at becker.robots.City.a(SourceFile:228)
at becker.robots.City.<init>(SourceFile:97)
at becker.robots.City.<init>(SourceFile:47)
at Starting_Template.main(Starting_Template.java:10)
This is a bug. See for example https://issues.jboss.org/browse/PLANNER-255?_sscc=t - the becker.jar has nothing to do with it.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Printing message on Console without using main() method
Can someone suggest how can a JAVA program run without writing a main method..
For eg:
System.out.println("Main not required to print this");
How can the above line be printed on console without using the public static void main(String arg[]) in the class.
Up to and including Java 6 it was possible to do this using the Static Initialization Block as was pointed out in the question Printing message on Console without using main() method. For instance using the following code:
public class Foo {
static {
System.out.println("Message");
System.exit(0);
}
}
The System.exit(0) lets the program exit before the JVM is looking for the main method, otherwise the following error will be thrown:
Exception in thread "main" java.lang.NoSuchMethodError: main
In Java 7, however, this does not work anymore, even though it compiles, the following error will appear when you try to execute it:
The program compiled successfully, but main class was not found.
Main class should contain method: public static void main (String[] args).
Here an alternative is to write your own launcher, this way you can define entry points as you want.
In the article JVM Launcher you will find the necessary information to get started:
This article explains how can we create a Java Virtual Machine
Launcher (like java.exe or javaw.exe). It explores how the Java
Virtual Machine launches a Java application. It gives you more ideas
on the JDK or JRE you are using. This launcher is very useful in
Cygwin (Linux emulator) with Java Native Interface. This article
assumes a basic understanding of JNI.
Up until JDK6, you could use a static initializer block to print the message. This way, as soon as your class is loaded the message will be printed. The trick then becomes using another program to load your class.
public class Hello {
static {
System.out.println("Hello, World!");
}
}
Of course, you can run the program as java Hello and you will see the message; however, the command will also fail with a message stating:
Exception in thread "main" java.lang.NoSuchMethodError: main
[Edit] as noted by others, you can avoid the NoSuchmethodError by simply calling System.exit(0) immediately after printing the message.
As of JDK6 onward, you no longer see the message from the static initializer block; details here.
public class X { static {
System.out.println("Main not required to print this");
System.exit(0);
}}
Run from the cmdline with java X.
Applets from what I remember do not need a main method, though I am not sure they are technically a program.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Printing message on Console without using main() method
Can someone suggest how can a JAVA program run without writing a main method..
For eg:
System.out.println("Main not required to print this");
How can the above line be printed on console without using the public static void main(String arg[]) in the class.
Up to and including Java 6 it was possible to do this using the Static Initialization Block as was pointed out in the question Printing message on Console without using main() method. For instance using the following code:
public class Foo {
static {
System.out.println("Message");
System.exit(0);
}
}
The System.exit(0) lets the program exit before the JVM is looking for the main method, otherwise the following error will be thrown:
Exception in thread "main" java.lang.NoSuchMethodError: main
In Java 7, however, this does not work anymore, even though it compiles, the following error will appear when you try to execute it:
The program compiled successfully, but main class was not found.
Main class should contain method: public static void main (String[] args).
Here an alternative is to write your own launcher, this way you can define entry points as you want.
In the article JVM Launcher you will find the necessary information to get started:
This article explains how can we create a Java Virtual Machine
Launcher (like java.exe or javaw.exe). It explores how the Java
Virtual Machine launches a Java application. It gives you more ideas
on the JDK or JRE you are using. This launcher is very useful in
Cygwin (Linux emulator) with Java Native Interface. This article
assumes a basic understanding of JNI.
Up until JDK6, you could use a static initializer block to print the message. This way, as soon as your class is loaded the message will be printed. The trick then becomes using another program to load your class.
public class Hello {
static {
System.out.println("Hello, World!");
}
}
Of course, you can run the program as java Hello and you will see the message; however, the command will also fail with a message stating:
Exception in thread "main" java.lang.NoSuchMethodError: main
[Edit] as noted by others, you can avoid the NoSuchmethodError by simply calling System.exit(0) immediately after printing the message.
As of JDK6 onward, you no longer see the message from the static initializer block; details here.
public class X { static {
System.out.println("Main not required to print this");
System.exit(0);
}}
Run from the cmdline with java X.
Applets from what I remember do not need a main method, though I am not sure they are technically a program.
I'm trying to open an URI with Swing that I get above error.
What is the reason and how can I fix it?
When I do it in console everything is OK but when I do in GUI I get this error.
I should say that I use Weblogic as server.
Code
private static void open(URI uri) {
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(uri);
} catch (IOException e) { /* TODO: error handling */ }
} else { /* TODO: error handling */ }
}
Stack trace:
Exception in thread "AWT-EventQueue-1" java.lang.NoClassDefFoundError: java/awt/Desktop
at be.azvub.ext.bcfidownloder.BcfiDownloadPanel.open(BcfiDownloadPanel.java:230)
at be.azvub.ext.bcfidownloder.BcfiDownloadPanel.access$000(BcfiDownloadPanel.java:37)
at be.azvub.ext.bcfidownloder.BcfiDownloadPanel$7.actionPerformed(BcfiDownloadPanel.java:147)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3129)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
Doc on NoClassDefFoundError
The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.
You do have some incorrect classloading happening. Mostly due to wrong class loader chaining.
NoClassDefFoundError can only be caused by a classpath problem.
Because Desktop is part of jre, make sure that your classpath contains a reference to the jre library.
In Eclipse, you can go to run configurations --> Classpath and check there
UPDATE:
As Andrew suggested, you can also check you have at least java 1.6
java.awt.Desktop has been introduced in Java 6. Chances are high you're running your code on different JRE versions.
This is the only error that I see in my code. It is this line:
public class VolCac extends JFrame implements ActionListener{
This is the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at VolCac.main(VolCalc.java:810)
This is line 810:
public static void main(String[] args){
JFrame frame = new CalcVolume();
frame.setSize(525, 350);
frame.setVisible(true);
}
}
I can’t figure why I’m getting the error.
"Unresolved compilation problem" errors at rntime are what happens when your code doesn't compile, but you tell Eclipse to run it anyway. The exception will happen at the point where the program can't continue any longer.
In this case, I see a file named "VolCalc," a class named "VolCac", and an attempt to create an instance of "CalcVolume". I'm pretty sure you mean for all three of these to match!
That error implies you're running Eclipse. Regardless, your IDE should be telling you what's wrong on that line. I hope you don't really have ALL that code on one line as you imply!
if you're using eclipse try to clean your project
and also your class name is VolCac and your are makine a new CalcVolume Object...
can't say much without seeing more code...