I have made a java application on netbeans. Ive tried converting it into .exe with the help of various softwares like exe4j and launch4j. But the problem is, these softwares take only 1 main class. My application has 3. Thus my application doesnt connect to the databsae !
-
https://www.youtube.com/results?search_type=search_videos&search_query=exe4j+tutorial&search_sort=relevance&search_category=0&page= – Madhawa Priyashantha Jul 01 '15 at 10:52
-
you can add all classes and you have to select main class – Madhawa Priyashantha Jul 01 '15 at 10:53
-
it didnt work. My application has 3 main classes. exe4j asks to select only 1 – Anmol Khanna Jul 01 '15 at 10:54
-
i have 4 classes in which 3 have main method. how am i supposed to select any 1 ? the application won't work then – Anmol Khanna Jul 01 '15 at 10:56
-
in exe4j you should first create a jar file – Madhawa Priyashantha Jul 01 '15 at 11:01
-
my jar file is already created. netbeans itself does that – Anmol Khanna Jul 01 '15 at 11:03
-
what do you mean by `these softwares take only 1 main class` you program can have only one main class . – Madhawa Priyashantha Jul 01 '15 at 11:06
-
my application has 4 programs out of which 3 have main classes ! – Anmol Khanna Jul 01 '15 at 11:08
-
You can define more than one main class in your application. But you have to pick one to launch it. – Modus Tollens Jul 01 '15 at 11:10
-
my application wont work then, because it wont be able to connect to the drivers and database – Anmol Khanna Jul 01 '15 at 11:11
-
@AnmolKhanna can you post a picture of your folder structure of netbean project – Madhawa Priyashantha Jul 01 '15 at 11:12
-
There can be only one main class used as entry point in a Java program. If you have three main classes and need all of them, you have created three Java applications and should probably create one exe for each of them. – Modus Tollens Jul 01 '15 at 11:14
-
http://imgur.com/ZDT7Fkh – Anmol Khanna Jul 01 '15 at 11:14
-
@KatjaChristiansen three exe's ? what do i do now ? obviously i need only one – Anmol Khanna Jul 01 '15 at 11:15
-
See [Using Multiple Main Classes](http://java.about.com/od/netbeans/a/multiplemainclasses.htm). Check your project properties in Netbeans to see which main class is selected in Netbeans and use that one. – Modus Tollens Jul 01 '15 at 11:20
-
@AnmolKhanna check my updated answer – Madhawa Priyashantha Jul 01 '15 at 11:22
-
Rewrite your code to start up database etc first. You can't have a jar with 3 main classes that get executed in the correct order. Thus you can't have an exe doing the same. – Schaka Jul 01 '15 at 11:26
-
@Schaka do i need to write all my code in a single class ? – Anmol Khanna Jul 01 '15 at 11:30
-
You need to re-write those additional main classes in a way that they can be executed by your main-main class. That way, because code is executed sequentially, it will start your servers before starting your application. – Schaka Jul 01 '15 at 11:53
-
and i have no idea how to do that. newbie here ! – Anmol Khanna Jul 01 '15 at 11:57
-
@AnmolKhanna from netbeans do you **manually** run all three main classes ?? even .jar has only one entry point . you can have 100s of classes with main but entry point is just one . ClassName.main(null) // (null String[] ) will work too if you want to call a main from another class – Srinath Ganesh Jul 01 '15 at 12:51
2 Answers
i personally use exe4j
in exe4j when you continue wizard you will get this kind of window
then you can click green + button to add your excitable jar file and in the genaral area you should specified the main class .in this example test
let's say your application name is newMDIApplication
in netbeans you have 3 classes
newMDIApplication.class
newJframe.class
newJframe2.class
when your application start main method of newMDIApplication.class
get executed .so when you make a jar file you should fill it's main method .
and in that exe4j window you should choose newMDIApplication
as main class

- 9,633
- 7
- 33
- 60
-
@AnmolKhanna ok so you have created a jar file.what happens when you double click it? – Madhawa Priyashantha Jul 01 '15 at 11:07
-
now i am asked to select a main class from the list. the problem is, all my programs are linked. if i select only one, the databse will not connect – Anmol Khanna Jul 01 '15 at 11:09
-
okay. i am not making the jar file, netbeans automatically does that. when i checked the project properties, it showed that application.NewJFrame. was the main method. should i change that now ? – Anmol Khanna Jul 01 '15 at 11:28
-
@AnmolKhanna yes it does but what i'm asking is when you double click that jar file what's happening does it open a gui or nothing happen ? – Madhawa Priyashantha Jul 01 '15 at 11:29
-
-
@AnmolKhanna well that mean your main method is not empty in `newMDIApplication` class. when your programe run your NewJFrame window opens so ,all you need to do is choose newMDIApplication.class as main class .it opens jframe – Madhawa Priyashantha Jul 01 '15 at 11:34
-
i tried that but instead of opening the newJFrame screen which is my login screen, the newMDIApplication screen opens with an error msg saying driver mismatch between dsn and application – Anmol Khanna Jul 01 '15 at 11:37
-
@AnmolKhanna `newMDIApplication` screen?? newframe and newframe2 classes are gui classes .but newMDIApplication.class is not a gui class right?can you tell what is contain inside main method of `newMDIApplication.class` – Madhawa Priyashantha Jul 01 '15 at 11:40
-
all three are gui classes. NewJFrame is my login screen, newMDIApplication is my form where i enter data and NewJFrame2 is a form that gets data from NewMDIApplication to print – Anmol Khanna Jul 01 '15 at 11:42
-
@AnmolKhanna what is the content of main method of `newMDIApplication.class` – Madhawa Priyashantha Jul 01 '15 at 11:43
-
@AnmolKhanna i think problem is that connection failed did you put your libries in the same folder where ur exe exist? – Madhawa Priyashantha Jul 01 '15 at 11:48
-
@AnmolKhanna what is the exact error you are getting when you open exe file – Madhawa Priyashantha Jul 01 '15 at 11:52
-
my login screen doesnt show up. and i get an error saying that there is a driver mismatch between dsn and application – Anmol Khanna Jul 01 '15 at 11:53
-
my login screen is newJFrame.. if i run the main method of newMDIApplication, how will the login screen come ? thus it shows me directly the form page of newMDIApplication with a connection error – Anmol Khanna Jul 01 '15 at 11:54
-
ok @AnmolKhanna do you think netbeans run your all guis simultaneously ? nope ..how did you make a jar file ?netbeans also ask main class when build jar file.i – Madhawa Priyashantha Jul 01 '15 at 11:57
-
i know that gui's run one at a time. my main method is set to application.NewJframe – Anmol Khanna Jul 01 '15 at 11:59
-
@AnmolKhanna then you should give that class as main class to exe4j . – Madhawa Priyashantha Jul 01 '15 at 12:00
-
is it possible somehow that i call newMDIApplication and newJFrame2 inside JFrame ? so that if i select my newJFrame, the rest are called automatically ? – Anmol Khanna Jul 01 '15 at 12:03
-
@AnmolKhanna see here http://stackoverflow.com/questions/8895823/the-specified-dsn-contains-an-architecture-mismatch-between-the-driver-and-appli – Madhawa Priyashantha Jul 01 '15 at 12:11
-
i know about this issue. but i m getting this error when i run the exe file. my netbeans project runs completely fine – Anmol Khanna Jul 01 '15 at 12:13
-
@AnmolKhanna may be your netbean runs under java 64 but your default system use java 32 .something like that . – Madhawa Priyashantha Jul 01 '15 at 12:17
-
that is not the problem. it uses 64 bit and i have 64 bit drivers – Anmol Khanna Jul 01 '15 at 12:24
-
@AnmolKhanna then why mismatch ?first read that question and answers .try to find your default java verstion – Madhawa Priyashantha Jul 01 '15 at 12:28
-
@AnmolKhanna if you can upload your project[with db] somewhere for instance in google driver so then i can find the problem – Madhawa Priyashantha Jul 03 '15 at 15:58
your question is little confusing .. the Mainclass means the class which contain public static void main(String[] args)
function void main is startup of the program. you can initialize your programs in void main..
void main function will called by JVM when execution has been started.. you can't use use multiple void mains because JVM gets confused with it lol
EDIT
now i understood your problem
first remove this code from all gui classes ..
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
find and add this code on your class constructor of all Gui classes, if your class name is NewJFrame then your class Constructor is like this
public NewJFrame() {
initComponents();
setVisible(true);; // add this code
}
if you having Gui classes only .. please add a Java Class and put public static void main function on it
Class AppleClass{
//import your classess here
//This is your main class
public static void main(String[] a){
//This function will execute first on program startup
//put your look and feel code here (optional, codes given below)
//Here you can put codes for which GUI you need to show first
}
}
you can show GUI by creating its object
Syntax
ClassName objectname = new ClassName();
ClassName - Name of your GUI Class ;
objectname - any name
for example :
NewJFrame abc = new NewJFrame();
look and feel code
// write it inside public static void main
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(JavaApplication10.class.getName()).log(Level.SEVERE, null, ex);
}
make sure these classes are imported
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.UIManager; import
- javax.swing.UnsupportedLookAndFeelException; import
- javax.swing.plaf.nimbus.NimbusLookAndFeel;
EDIT 3 - Converting into exe
- Build executable jar , goto run menu-> Clean and Build Project(Project name)
- Download Launch4j and run it
- set Output exe file location
- set Executable Jar location ( you can find it on dist folder in Project directory)
- goto 'class path' tab uncheck 'custom path'
- goto 'Single Inheritance' tab and uncheck 'Allow only a single......'
- goto 'JRE' tab set min version-1.5.0_45 , max-version -1.8.0_45
- click gear icon and save configration
- click run button

- 813
- 9
- 14
-
-
-
@AnmolKhanna no you so misunderstand.do you think your all guis open s simultaneously.there is a one class which open star your programe – Madhawa Priyashantha Jul 01 '15 at 11:50
-
-
okay @Sajan i tried your way, but after running the exe file i get a architecture mismatch between driver and app. – Anmol Khanna Jul 01 '15 at 12:27
-
load all the resources (arch ,drivers...) and try . or better post source code and sequence of execution of those classes – Srinath Ganesh Jul 01 '15 at 12:53
-
@Sajan by your method, all my gui's open at once ! i have to open them one by one ?! – Anmol Khanna Jul 02 '15 at 06:21
-
@Sajan still i get this error. its stlll looking for the main method inside NewJFrame and not my new main class(Say first) Error: Main method not found in class application.NewJFrame, please define the main method as: public static void main(String[] args) – Anmol Khanna Jul 02 '15 at 06:31
-
@AnmolKhanna could you please post source codes of all classes .and explain. program flow.. ? – Sajan Jul 04 '15 at 07:11