Using External .jars compile time vs run time - java

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

Related

"The type javax.swing.JFrame is not accessible" What does this mean and how do I fix it?

I'm following along with this tutorial, and I got a problem early in the video (at approximately 7:45). I'm trying to create a basic Java program that will launch a window, however, I can't seem to import JFrame.
I've looked for other solutions on Stack Overflow, but I haven't found one that works for me.
Here is the code I've written:
import javax.swing.JFrame;
public class App {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello World");
frame.setVisible(true);
}
}
I'm using Eclipse version 4.12.0 on a Macbook Pro (13-inch, Mid 2012) running macOS Mojave version 10.14.5
Expected result: A window opens when I run the program, and when I close the window, the program ends.
Actual result: No window is created and I get this error message:
Error occurred during initialization of boot layer
java.lang.module.FindException: Error reading module: /Users/username/eclipse-workspace/Swing1/bin
Caused by: java.lang.module.InvalidModuleDescriptorException: App.class found in top-level directory (unnamed package not allowed in module)
If you have a module-info.java file, put this in the module:
requires java.desktop;
If you have created the java app with eclipse your fault is the package.
With eclipse, I created a java app and this is the result
This code fixed your foult
package demo;
import java.awt.Dimension;
import javax.swing.JFrame;
public class App {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello World");
frame.setLocationRelativeTo(null);
frame.setSize(new Dimension(400, 400));
frame.setVisible(true);
}
}
The reference for understand the package
I had the same issue. I did similar code in Eclipse. I got the error The type javax.swing.JFrame is not accessible on the side of import javax.swing.JFrame;
The solution is:
Delete the line of import javax.swing.JFrame;
And then, inside your body of your code inside your main, with your mouse, hover over JFrame keyword, and Eclipse will offer some auto-completion suggestion.
Select import 'JFrame' (javax.swing)
This will bring the required import automatically. It is a kind of shortcut.
In order to avoid these type of errors: Never type manually, get the imports AND methods, for example setVisible by autofilling. For instance, type frame.setV and again Eclipse will suggest the completion ... select from there. I do not know why, but this is what happened in my case.

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

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.

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.

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.

Categories

Resources