Why can't I run my .jar file normally? (MacOS) - java

I just programmed my first JFrame and exported it, but I'm not able to run the .jar file normally. When I try to run it using Terminal, it works without any problems. However, when I try to open the file normally by double clicking on it, I get an error message similar to this: The Java jar file "Programm.jar" could not be launched.
Here's the code (maybe there's something wrong with it?):
package GUI;
import javax.swing.JFrame;
public class Frame {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setVisible(true);
jf.setSize(500, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setTitle("Programm");
jf.setResizable(false);
}
}
Thanks in advance!

I think you need to associate the file type with the java executable, then the Finder will know what to do when you double click

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.

How does intelliJ process and compile Swing app code?

I tried building simple Swing forms using IntelliJ's GUI designer tool and noticed some things I don't like (at least till I don't understand). For this question lets consider a very simple Swing form with just one button. I opened up GUI designer and added a button( 'load file') to the middle of mainPanel using GridBagLayout. The java code it generated (I wrote the main method myself):
package mainPackage;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Created by Povilas on 06/01/2017.
*/
public class MainFrame {
private JButton loadFileButton;
private JPanel mainPanel;
public MainFrame() {
loadFileButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"This should be a browser dialog...");
}
});
}
public static void main(String[] args) {
MainFrame mainFrame = new MainFrame();
JFrame frame = new JFrame("App");
frame.setContentPane(mainFrame.mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
If i run this code it works, which is crazy and unexpected, because nowhere in the code loadFileButton is initialized, nowhere in the code mainPanel is initialized, nowhere in thecode is LayoutManager added or button added to the panel. If i tried compiling this code from cmd I would get NullPointer exceptions in the constructor because button and panel are not initalized.
Question:
How come intelliJ compiles and runs the app successfully? Does it process .form file at compile time to add missing code? I want to design Swing dialogs with IntelliJ's designer but to get proper, full code which could then be compiled and packaged a from cmd using javac and jar, how do I do that?

java System.setProperty not affecting anti-aliasing; command line option works

I'm trying to make a java Swing program default to anti-aliasing when run on Linux. It works when I pass the settings as command line options or using _JAVA_OPTIONS, but not using System.setProperty().
I'm using Java SE 6 on Centos 5.5 via VNC with gnome metacity WM.
public class Test {
public static void main(String args[]) {
System.setProperty("useSystemAAFontSettings", "lcd");
System.setProperty("swing.aatext", "true");
JFrame frame = new JFrame("Test");
JButton button = new JButton("Button");
frame.getContentPane().add(button);
frame.setSize(100,100);
frame.setVisible(true);
}
}
Any clues what I'm doing wrong? Thanks.
PS: The swing.aatext setting seems to have no effect in any case, but I included it as I saw it as part of the solution elsewhere.

Why is my Java swing window not showing up?

so I'm starting to learn Java Swing, following a YouTube video.. installed Java 8 and NetBeans 8.0 in Linux Ubuntu 14.04. Made a new Java application, and wrote the following code:
package basicswing;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
public class BasicSwing extends JFrame {
JPanel p = new JPanel();
JButton b = new JButton("Hello");
public static void main(String[] args) {
new BasicSwing();
}
public BasicSwing() {
super("Basic Swing App");
setSize(400,300);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p.add(b);
add(p);
setVisible(true);
}
}
When I clean, build, and then run the project, it shows that a Java project is running, but I don't see the Frame, it doesn't pop up.. I don't actually think it's a code issue, I've tried it with different code, and the frame still doesn't show. I can't figure out if it's a NetBeans issue or a Java issue.. or maybe something else?
It is an environment (NetBeans maybe?) issue. Try running from command line:
$ javac BasicSwing.java
$ java BasicSwing
The code is correct and displays the frame.

My jar file runs but nothing comes up

I have been searching the internet, but I can't find a solution for this. My program works fine in Eclipse, but I have to make the program into a .jar file so the user can just click on the .jar file and the GUI will run.
When I click on the .jar file that I exported, nothing happens. There is just the main class of the program and if I click run in Eclipse, the GUI will come up.
What do I need to add in here so the GUI will stay and not disappear straight away? I tried to put a JOptionPane in there and it works, the box will come out but the GUI will not still.
package dijkstra;
import java.awt.BorderLayout;
import java.util.Map;
import javax.swing.JFrame;
public class RunGUI {
public static void main(String[] args){
FlightSchedulerGUI.setWindowsLookAndFeel();
try {
Map<Integer, Airport> airports = FileProcess.loadtegMap();
FlightSchedulerGUI GUI = new FlightSchedulerGUI(airports);
GUI.randomizeRoute();
GUI.findBestRoute();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(GUI);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Flight Scheduler");
frame.setSize(600,400);
frame.setVisible(true);
} catch(Throwable t ){
t.printStackTrace();
System.err.flush();
System.exit(1);
}
}
}
Ensure you have added the Main-Class attribute to the MANIFEST file of your jar file.
Example:
Main-Class: dijkstra.RunGUI
The file must be called MANIFEST.MF and must be placed to META-INF folder, within your jar file.
More info

Categories

Resources