SWT in eclipse Java JDE not working - java

When I input the following Java code into eclipse, it returns an error. I am told by the eclipse tutorials that this should work. What am I doing wrong?
This is a picture of my code.
import org.eclipse.swt;
public class SWTHELLOWORLD{
public static void main(String[] args){
Display display=new Display();
Shell shell = new Shell(display);
shell.setText("Hello world");
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())display.sleep();
}
display.dispose();
}
}
When I run as a java application, it returns this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Display cannot be resolved to a type
Display cannot be resolved to a type
Shell cannot be resolved to a type
Shell cannot be resolved to a type

You are not importing the Display and Shell classes.
You should add the following imports to the top of your class:
import org.eclipse.swt.widgets.Display
import org.eclipse.swt.widgets.Shell
Just importing org.eclipse.swt will not import all the classes that you need.

You used
public static void main (Strings[] args)
while the correct way of putting it would be
public static void main (String [] args)
Mind that it is String without the 's' in the end.

Related

How to get Lightrun running in IntelliJ IDEA 2022.3.1?

No matter what I do Lightrun will generate the following error message:
General agent error at jvm_internals.cc:186.
This is my first time using Lightrun.
This is my code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println(new Scanner(System.in).nextInt() + new Scanner(System.in).nextInt());
}
}
The code itself runs perfectly.
I am using IntelliJ IDEA 2022.3.1.
LR doesn’t work on this version. A bit buggy on the ij versions for the past 2 months…

Instantiating a Java AWT object causes empty application to open on macOS

When I instantiate an object from java.awt, the program causes a new macOS application named "Java", with no windows, to open.
How can I prevent this from happening?
Here is a minimal example:
import java.awt.Rectangle;
public class Main {
public static void main(String[] args) {
Rectangle rect = new Rectangle();
}
}
After compiling and running it in the most canonical way (javac Main.java; java Main), the following icon appears in the Dock: screenshot
I've traced the code, and the offending method is Toolkit.loadLibraries() (JDK 1.8.0_172-b11).
I found the solution based on #MadProgrammer's comment.
The answer is to set AWT to headless mode.
When executing the program:
$ java -Djava.awt.headless=true Main
Or programatically:
System.setProperty("java.awt.headless", "true");

Using External .jars compile time vs run time

I'm trying to break into java and game programming using opengl. I built jogl and gluegen using ant and all junit tests ran successfully. However, when I attempt to run a simple OpenGL test program it gives me a "Could not find or load main class..." error. When compiling the program I enter:
javac -cp etc...\jogl-all.jar;etc...\gluegen-rt.jar filename.java
into the command line and I receive no compile-time errors. Then I attempt to run the program using:
java -cp etc...\jogl-all.jar;etc...\gluegen-rt.jar filename
and it tells me it cannot locate the main class.
Is this an error caused by using the external .jars? Or am I not using the -cp function properly? Any help would be appreciated!
The following is the code I'm trying to run:
import com.jogamp.opengl.*;
import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.common.type.*;
public class SimpleScene {
public static void main(String args[]) {
GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);
GLWindow window = GLWindow.create(caps);
window.setSize(300, 300);
window.setVisible(true);
window.setTitle("NEWT Window Test");
window.addWindowListener(new WindowAdapter() {
public void windowDestroyNotify(WindowEvent arg0) {
System.exit(0);
};
});
}
}

Java helloworldswt tutorial doesn't work

I am new to Java/SWT/Eclipse and trying to learn SWT and I am having difficulties to get the following code working.Can someone help ?
Code:
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloHelloSWT {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Hello world!");
shell.open();
while(!shell.isDisposed()){
display.sleep();
}
display.dispose();
shell.close();
}}
Well I tried running it the same way as the first tutorial, but this error popped up:
Error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons:
no swt-win32-4333 in java.library.path
no swt-win32 in java.library.path
Can't load library: C:\Users\sdp0121\.swt\lib\win32\x86\swt-win32-4333.dll
Can't load library: C:\Users\sdp0121\.swt\lib\win32\x86\swt-win32.dll
at org.eclipse.swt.internal.Library.loadLibrary(Library.java:331)
at org.eclipse.swt.internal.Library.loadLibrary(Library.java:240)
at org.eclipse.swt.internal.C.<clinit>(C.java:21)
at org.eclipse.swt.widgets.Display.<clinit>(Display.java:138)
at HelloHelloSWT.main(HelloHelloSWT.java:8)
Go back to the step of the tutorial titled "Configure the Java project". Then go into the project Properties and select Java Build Path then expand the reference to the SWT project (small triangle before the project name). If it says "None", click Edit and find the SWT project in my workspace.
Hope this helps.

Why my SWT application does not work?

I am trying to create a Hello World SWT application using Eclipse. I follow all instructions and in the end my program does not work.
This is my code:
import gnu.gcj.xlib.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloWorldSWT {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Hello world!");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
And this is my error messages:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Shell(Display) is undefined
The method readAndDispatch() is undefined for the type Display
The method sleep() is undefined for the type Display
The method dispose() is undefined for the type Display
at HelloWorldSWT.main(HelloWorldSWT.java:13)
Does anybody know how I can check what is wrong?
I think you're importing a wrong Display class. The right one should be
org.eclipse.swt.widgets.Display
Clean all in your folder, do it again, import swt create proj, check build path add class, run
this should work.
If not , right click , click clean up, click source... organize imports, run again. Should work, if error no
swt.dll
in your library, copy all
swt.dll
to your library path. Should work now.
I made the same mistake. My issue was on the second step: I selected to import "org.eclipse.swt". Instead, you must select the correct one for your operating system. In my case, this was "org.eclipse.swt.win32.win32.x86". Once you've done this, the rest of the steps in the tutorial should work as expected.
The answers above, while correct, may assume a bit more knowledge than most completing this tutorial will have.

Categories

Resources