So I've ran into the problem with creating a working .jar file. My initial goal was to create the application, a simple one, with barely any functional, one you can just click on and launch without ide or console. Like a notepad for example. But this is where the problems began. I thought I could create the .jar file and then an .exe out if and test. But none of it seemed to work. Here is source file from which I compiled .class and then made a .jar file:
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.*;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class GuiScreenLink extends Frame implements ActionListener
{
Button OpenLink, TakeScreenshot;
Desktop desktop;
URI link;
BufferedImage img;
Robot robot;
File newFile;
public GuiScreenLink()
{
setLayout(new FlowLayout());
OpenLink = new Button("OpenLink");
TakeScreenshot = new Button("TakeScreenshot");
OpenLink.setBackground(Color.WHITE);
TakeScreenshot.setBackground(Color.WHITE);
add(OpenLink);
add(TakeScreenshot);
try {
link = new URI("https://www.google.com/");
} catch (URISyntaxException ex) {
Logger.getLogger(GuiScreenLink.class.getName()).log(Level.SEVERE, null, ex);
}
OpenLink.addActionListener(this);
TakeScreenshot.addActionListener(this);
addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
/**
*
* @param ae
*/
@Override
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if(str.equals("OpenLink"))
{
desktop = Desktop.getDesktop();
try {
desktop.browse(link);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
else if(str.equals("TakeScreenshot"))
{
try
{
robot = new Robot();
} catch (AWTException e) {
throw new RuntimeException(e);
}
img = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
newFile = new File("Screenshot");
try
{
ImageIO.write(img,"png", newFile);
} catch (IOException e)
{
throw new RuntimeException(e);
}
}
repaint();
}
public static void main(String[] args)
{
GuiScreenLink app = new GuiScreenLink();
app.setSize(new Dimension(200,200));
app.setTitle("Keys");
app.setBackground(Color.DARK_GRAY);
app.setVisible(true);
}
}
Then i did this:
javac *.java
jar -cfv program.jar *.class
So after I try to execute the .jar file nothing happends at all. However in IDE everything works. Is there any solution to that problem? And if there is then how can I make a .exe file out of it afterwards to launch it on windows?
CodePudding user response:
First of all, a JAR is never executable on its own but needs a Java Runtime Environment. Up to Java 8 a JRE was distributed. Users installed that and from that point on, they could launch 'executable JARs' by double-clicking the JAR-file on Windows.
An 'executable JAR' has to specify the main class in it's manifest as shown e. g. in the Java Tutorial.
In newer versions of Java, you can create a module and use that module to create a custom runtime image using a tool called jlink
(see e.g. https://www.baeldung.com/jlink). jlink
also creates launchers if desired.
Using jpackage
you could create native images (s. e.g. https://www.baeldung.com/java14-jpackage).
CodePudding user response:
To execute a jar-file with the -jar parameter, you have to add a manifest to the jar-file that specifies at least, which is the class containing the main method.
To do that, you can simply add -e=CLASSNAME
or --main-class=CLASSNAME
to the call of jar
.
You find the full description of the jar tool in the Java® Development Kit Version 17 Tool Specifications at the oracle docs page.
But I rather would recommend that you start using a build tool like Apache Maven, that will handle dependencies, build and packaging (= creating the jar file) for you.