Home > Default > New instances of main class

New instances of main class

November 30Hits:0
Advertisement
Hi People.
I'm currently programming a toolbox of applications in Java - each tool is a main program in its own right (i.e. has main method). I'm also programming an organiser which has buttons, which will (eventually) run the main programs i have already mentioned 'on demand'. I've tried using ClassLoader etc. but keep getting errors (see below). Does anyone have a better solution, please? Links to helpful sites will be much appreciated!
By the way, i'm fairly new to Java so be gentle! :)
Cheers, folks.
ERRORS I GET WHEN USING CLASSLOADER (and i'm 100% sure i've got the file name and directory correct):
java.io.FileNotFoundException: (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:64)
at java.io.FileReader.<init>(FileReader.java:38)
at TestClass.main(TestClass.java:9)

Answers

Ok now I think you have a fundamental flaw in your design, and also your understanding of object oriented programming.
Here's what I think you want to achieve:
-A series of components which can be accessed in two ways - either independently through running "java classname" on them, and also through a container which lets you access them via buttons.
This is not a hard problem, and here is my (simplified) solution in code:
File: Component1.java
public class Component1 extends JFrame{
//do some stuff in here specific to this component
public static void main(String[] args){
  Component1 comp1 = new Component1();
  comp1.show();
}========================================
File: Component2.java
public class Component2 extends JFrame{
//do some stuff in here specific to this component
public static void main(String[] args){
  Component2 comp2 = new Component2();
  comp2.show();
}========================================
File: Container.java
public class Container extends JFrame{
public Container(){
  //get a reference to the components
  Component1 comp1 = new Component1();
  Component2 comp2 = new Component2();
//Button click listener code
public void onClick(Component comp){
  comp.show();
//do some stuff in here specific to this component
public static void main(String[] args){
  Container cont = new Container();
  cont.show();
}Ok so I simplified it a lot and missed out all the button listener stuff (plus the design would be a lot nicer using interafces for the components...but one step at a time!) but it should show you how main methods interact with classes in java.
Please reply if you still dont understand, or this is wrong!

Read other 15 answers

Tags:

Related Articles

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 1.043 s.