Java helloworldswt tutorial doesn't work - java

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.

Related

How to fix Java error "Unresolved compilation problem" in Eclipse IDE?

I'm trying to dabble with a basic ComboBox (i hear these are very outdated). The goal is to create a box with options for the user and when they click said option in the ComboBox they will get information from an array of strings based on the case detected by the switch. This is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SolicitorComBox extends JFrame implements ActionListener {
String[] MuniArray = {"Allepo", "Avalon"};
JComboBox MuniList = new JComboBox (MuniArray);
JLabel lblText = new JLabel();
public static void main (String[] args) {
SolicitorComBox fr = new SolicitorComBox();
centerFrame(fr);
fr.setVisible(true);
}
private static void centerFrame(SolicitorComBox fr) {
// TODO Auto-generated method stub
}
public SolicitorComBox() {
setLayout (new FlowLayout());
setSize (400, 300);
setTitle ("Solicitor Search");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MuniList.setSelectedIndex(0);
MuniList.addActionListener(this);
add(MuniList);
add(lblText);
}
public void actionPerformed(ActionEvent e) {
if (e.getsource() == MuniList) {
JComboBox cb = (JComboBox).e.getsource();
String msg = (String)cb.getSelectedItem();
switch (msg) {
case "Allepo": lblText.setText("The attorney is Joe!");
break;
case "Avalon": lblText.setText("The attorney is Dana!");
break;
This is the error I'm getting when I try to run the code:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at SolicitorComBox.main(SolicitorCombobox.java:10)
beside the fact you didn't post your whole code, I was able to run the piece of code you put over there:
So or your issue is below the code you posted, or it is just a compilation problem in your IDE
I would recommend to you manually refresh your project and force clean it using the menu Project > Clean in Eclipse. I will force your app to recompile.
I had a similar problem. I was using an imported gradle project but running it from inside eclipse. For some reason the error suddenly started to appear.
Solution 1. In eclipse click clean then refresh. This worked, but not for very long
Solution 2. Gradle has an eclipse task for interacting with the eclipse environment and making sure its libs are correct: So running gradle clean build eclipse seemed to provide a solution. Presumably maven has a similar task.
you may be missing some jar files. Right click on your project or class, goto build path and select "build path configuration". from the "build path" window, select "libraries" tab, find the missing files, download them and put the in your project from the "project explorer" side. you can use drag and drop.
Now click on "Add JARs" to add the jar files to the library from the project.
it worked for me. hope it works for you.
To fix Java error “Unresolved compilation problem” in Eclipse IDE follow these steps :
Try to include package name in eclipse in case if you forgot it
Import all packages before using it, EX: import java.util.Scanner before using Scanner class.
Also make sure to check compiler compliance level and selected jdk version is same
I eclipse, if you skip including package it will give "Unresolved compilation problem"

SWT in eclipse Java JDE not working

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.

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);
};
});
}
}

UnsatisfiedLinkError no jogl in java.library.path

I'm trying to build a jogl app. I downloaded the jars and the native dll files. I have included them in my buildpath but when I run my code I get a the error from the title
Here is my vm file:
-server
-Xms128m
-Xmx512m
-XX:MaxPermSize=250m
-XX:ReservedCodeCacheSize=64m
-ea
-Dsun.io.useCanonCaches=false
-Djava.net.preferIPv4Stack=true
-XX:+UseCodeCacheFlushing
-XX:+UseConcMarkSweepGC
-XX:SoftRefLRUPolicyMSPerMB=50
-Djava.library.path="C:\Users\Vlad\Documents\dev\jogamp-all-platforms\lib\windows-amd64"
Here is that folder:
Here are the jars that I have in my build path:
And finally if there is any need for the actual code here it is:
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.swing.*;
/**
* on 20/02/14.
*/
public class Demo extends JFrame {
static Demo app = new Demo();
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
app.setVisible(true);
}
});
}
public Demo(){
super("This is my first jogl app");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GLCapabilities caps = new GLCapabilities();
GLCanvas canvas = new GLCanvas(caps);
canvas.addGLEventListener(new MyGLListener());
getContentPane().add(canvas);
}
}
EDIT
I have changed the libraries to match the new ones:
As you can see I have the natives and the jogl-all.jar and even the gluegen-rt.jar library.
The error that I get now is a compiler error:
This is the piece of code that's causing it:
GLCanvas canvas = new GLCanvas(new GLCapabilities());
It says that GLCapabilites (GLProfiles) in GLCapabilties cannot be applied to ();
You may also need to include gluegen-rt.jar in your build path. You should be able to obtain this from the same place where you found jogl-all.jar (JOGL 2).
Regarding your edit, for simplicity, you can use:
GLCanvas canvas = new GLCanvas(new GLCapabilities(null));
This will allow you to use the default GLProfile.
Your source code uses JOGL 1 whereas you try to use JOGL 2 JARs with it. Rather look at my simple example on Wikipedia. You can find the instructions to install JOGL in our Wiki here.

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