• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Executing many main methods in a jar file  RSS feed

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I'm new to Jar files and I've scoured the net for the answer to this question.

My jar has many class files and 3 main methods in total. I want to know if I can run a specific main method from a class inside a jar. The jar does not have a manifest file. The main method is chosen to run from another program at run time.

Thanks!
Rachel
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main method is a starting point to run the program. You can have only 1 main method class from where the execution begins. Why do you want to have more than one main method?
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rachel,

Yes, you can. If the jar file contains classes A, B, and C, each with its own main(), then you can run them individually just by using

java -classpath myJarFile.jar A
java -classpath myJarFile.jar B
java -classpath myJarFile.jar C

If there were a manifest, it could point to one of these three to run by default when the jar was double-clicked, but you'd still be able to run the three programs separately as above.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spot on Ernest! That works beauitifully!!! Hooray! Damn jar files!

Ali, the reason that I need more than one main method is because the java that I' writing is the backend to a VB front end, so I need different things to happen in the backend when buttons are pushed. I'm still investigating the posibility of having a thread or something listening for the buttons but I haven't got there yet. As for why the front end in VB - I don't know, I just do what I'm told!



Thanks guys!
Rachel
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there, I found a different problem that I have now. When I run the command from the command line, it works only if I execute the cmd from the same folder that it is stored in. Problem is that I need to call the cmd from 2 folders above it.

Meaning that my jar is in \ss\server\lib but I want to run the command from \ss\

Do you have any suggestions?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as you include the full path to the jar file on the command line, it should work fine from anywhere (unless, of course, the app is written to expect some other files in the current directory -- but that's a separate discussion.)
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! That works well.

Thanks again!
Rachel
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!