Reputation: 323
I have use eclipse export and exported this as jar file, but it's working fine inside the eclipse test run, but not working as jar file, any one help me? (i'm new for java, this is my first app)
package com.java.folder;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class javafolder extends JApplet implements ActionListener{
private String textLine = "";
JButton b1, b2;
TextField text1, text2;
Container content = getContentPane();
public void init() {
text1 = new TextField(8);
text2 = new TextField(8);
JLabel label = new JLabel("Folder Name");
label.setFont(new Font("Serif", Font.PLAIN, 12));
content.add(label);
add(text1);
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
b1 = new JButton("Creat A Folder");
b1.addActionListener(this);
content.add(b1);
b2 = new JButton("Creat A Folder");
b2.addActionListener(this);
//content.add(b2);
}
// Called when the user clicks the button
public void actionPerformed(ActionEvent event) {
String s;
textLine = event.getActionCommand();
s = text1.getText();
String path = System.getProperty("user.dir");
File dir=new File(path+"/"+s);
if(dir.exists()){
JOptionPane.showMessageDialog(null, "Folder Name Already Exists!!! :(", "Error",
JOptionPane.ERROR_MESSAGE);
}else{
dir.mkdir();
JOptionPane.showMessageDialog(null, "Folder Created :) Folder path:"+dir, "INFORMATION_MESSAGE",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
Upvotes: 1
Views: 12112
Reputation: 9323
You have written an applet, not a "runnable desktop application", so you can export it as a jar but to execute it, you must use the "appletviewer" tool provided with the JDK, or a browser with Java support.
However a Swing applet is not very different by a small Swing desktop application. The fundamental difference is that an application must have a "main" method, that is a method with this signature:
public static void main(String [] args)
You can convert your applet to an application with 3 simple changes:
1) Your class must extend JFrame
instead of JApplet
, so change the class declaration in this way:
public class TestSwing extends JFrame implements ActionListener { ... }
2) Add the following main()
method:
public static void main(String[] args) {
TestSwing myApp = new TestSwing();
myApp.init();
}
3) Add the following lines to your init()
method:
setSize(new Dimension(760, 538));
setTitle("My App Name");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
That's all.
Upvotes: 2
Reputation: 93872
Right click on your Project -> Export -> Java -> Runnable JAR file
From command line :
java -jar myJar.jar
Upvotes: 3