This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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
Posts: 24203
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
Posts: 24203
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!