Need Help Running a Program from Another Program

Simple issue, I think, just I have no idea where to start or what meth

Page 1 of 1

10 Replies - 1062 Views - Last Post: 29 June 2009 - 12:43 PM Rate Topic: -----

#1 wolfman29   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 24-June 09

Need Help Running a Program from Another Program

Posted 28 June 2009 - 06:50 PM

My question is pretty simple - I wrote up a great standalone program, which runs like a charm. However, because this is only going to be a small part of the major project I am doing, I want to be able to have the standalone programs be separate from the "mother" program, so that the mother program isn't filled with that much code.

Here is the mother program I am testing right now:

import calculator.*;

public class testImport {

	public static void main(String[] args) {

	singleVariableCalcPackage algebraCalc = new singleVariableCalcPackage();

	}

}


Basically, I want it to look something like this:

import calculator.*;

public class testImport {

	public static void main(String[] args) {

	singleVariableCalcPackage.somethingThatRunsTheProgram();

	}

}


Or at least something like that. Basically, the "singleVariableCalcPackage" is a program with a GUI and everything, and I want to be able to put THAT GUI into another GUI that has tabs and everything, so people can switch between multiple different calculators. So it would look like a bunch of tabs, and when you click on one tab, it automatically runs one GUI in the main GUI (like the calculator GUI would be inset INSIDE the mother GUI), and then you could do all the functions the standalone could do.

Then you could go to another tab, do the same thing, etc.

However, I have no idea where to begin. Anyone want to help?

NOTE: THIS IS NOT A HOMEWORK ASSIGNMENT!

Is This A Good Question/Topic? 0
  • +

Replies To: Need Help Running a Program from Another Program

#2 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Need Help Running a Program from Another Program

Posted 28 June 2009 - 09:42 PM

hello wolfman29, you mean you just want to execute the standalone program from your program? Is the program you want to execute an .exe or script? In that case you can use:
Runtime.getRuntime().exec("myProgram.exe")

I hope that this is what you were looking for, good luck ;)
Was This Post Helpful? 0
  • +
  • -

#3 wolfman29   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 24-June 09

Re: Need Help Running a Program from Another Program

Posted 28 June 2009 - 11:45 PM

View Postayman_mastermind, on 28 Jun, 2009 - 08:42 PM, said:

hello wolfman29, you mean you just want to execute the standalone program from your program? Is the program you want to execute an .exe or script? In that case you can use:
Runtime.getRuntime().exec("myProgram.exe")

I hope that this is what you were looking for, good luck ;)


Unfortunately, no, it is not an .exe, it is a .java/.class. So I am trying to get it to run the .class file IN the mother program's GUI.
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Need Help Running a Program from Another Program

Posted 28 June 2009 - 11:49 PM

Have an Action to create an instance of it and set it to visible since I believe you did say it was a GUI as well.

singleVariableCalcPackage algebraCalc = new singleVariableCalcPackage(); algebraCalc.setVisible(true);

That will create an instance of it and have it run just like a stand alone program. Don't forget to change the DefaultCloseOperation to DISPOSE_ON_CLOSE instead of exit or will exit entire program.

Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#5 wolfman29   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 24-June 09

Re: Need Help Running a Program from Another Program

Posted 29 June 2009 - 12:25 AM

View PostFuzzyness, on 28 Jun, 2009 - 10:49 PM, said:

Have an Action to create an instance of it and set it to visible since I believe you did say it was a GUI as well.

singleVariableCalcPackage algebraCalc = new singleVariableCalcPackage(); algebraCalc.setVisible(true);

That will create an instance of it and have it run just like a stand alone program. Don't forget to change the DefaultCloseOperation to DISPOSE_ON_CLOSE instead of exit or will exit entire program.

Hope this helps!


Just tried this out:
import calculator.*;

public class testImport {

	public static void main(String[] args) {

		singleVariableCalcPackage algebraCalc = new singleVariableCalcPackage();
		algebraCalc.setVisible(true);

	}

}


It compiled and ran quite nicely, just didn't actually do anything. I just typed "jdk1.6.0_14\Bin\java testImport", I waited a couple of seconds, and then it went to the next command line. Absolutely nothing happened.

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#6 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Need Help Running a Program from Another Program

Posted 29 June 2009 - 12:38 AM

can you show us your singleVariableCalcPackage class? Did you make sure to set the size and position of the window object algebraCalc in your main? If you did not set the size the window will not appear correctly.... e.g.try:
algebraCalc.setSize(400,400);

Good luck :)
Was This Post Helpful? 0
  • +
  • -

#7 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Need Help Running a Program from Another Program

Posted 29 June 2009 - 04:10 AM

This is what I would do. In your calculator class, if i remember you add your JPanel to a JFrame. Remove the code where you add it to a JFrame and just keep it for now just on a panel.
Now, create a new class, and have it extend a JPanel or JFrame. Create a TabbedPane. Add your claculator panels to the tabs you create. I have provided you with a little example of how you might create one tab and add your calculator panel to it. Have a try with it.
import java.awt.event.KeyEvent;
import javax.swing.*;
import java.awt.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * JTabbedPane class.  All JPanels are brought together here and placed on a JTabbedPane
 * 
 * 
 * @author 
 * @version 1.00 2009/3/23
 */
public class TabbedView extends JPanel { 

	private JTabbedPane tabbedPane;
	private singleVariableCalcPackage algebraCalc = new singleVariableCalcPackage();
	
   /**
	* Class Constructor which sets up the JPanel and calls createGUI method.
	*/
	public TabbedView() {
		super(new GridLayout(1, 1));
		
		setLocation(new Point(0, 0));  
		setVisible(true); 			
		setSize(800, 610);			
		tabbedPane = new JTabbedPane();
		mainPanel();
	}

   /**
	* Sets up each JPanel to be placed on appropiate Tab
	*/
	private void mainPanel(){
		
		JComponent panel1 = new JPanel();
	   	panel1.setBackground(new Color (31, 89, 99));
		panel1.setLayout(new BorderLayout(5, 5));
		panel1.add(algebraCalc, BorderLayout.CENTER);
		tabbedPane.addTab("Tab 1", panel1);
		tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
		//Add more of the above code block for more tabs
		
		
		
		add(tabbedPane);
		
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
 
	}
	

	
	
}
	

Was This Post Helpful? 0
  • +
  • -

#8 wolfman29   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 24-June 09

Re: Need Help Running a Program from Another Program

Posted 29 June 2009 - 07:42 AM

View Postnick2price, on 29 Jun, 2009 - 03:10 AM, said:

This is what I would do. In your calculator class, if i remember you add your JPanel to a JFrame. Remove the code where you add it to a JFrame and just keep it for now just on a panel.
Now, create a new class, and have it extend a JPanel or JFrame. Create a TabbedPane. Add your claculator panels to the tabs you create. I have provided you with a little example of how you might create one tab and add your calculator panel to it. Have a try with it.
import java.awt.event.KeyEvent;
import javax.swing.*;
import java.awt.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * JTabbedPane class.  All JPanels are brought together here and placed on a JTabbedPane
 * 
 * 
 * @author 
 * @version 1.00 2009/3/23
 */
public class TabbedView extends JPanel { 

	private JTabbedPane tabbedPane;
	private singleVariableCalcPackage algebraCalc = new singleVariableCalcPackage();
	
   /**
	* Class Constructor which sets up the JPanel and calls createGUI method.
	*/
	public TabbedView() {
		super(new GridLayout(1, 1));
		
		setLocation(new Point(0, 0));  
		setVisible(true); 			
		setSize(800, 610);			
		tabbedPane = new JTabbedPane();
		mainPanel();
	}

   /**
	* Sets up each JPanel to be placed on appropiate Tab
	*/
	private void mainPanel(){
		
		JComponent panel1 = new JPanel();
	   	panel1.setBackground(new Color (31, 89, 99));
		panel1.setLayout(new BorderLayout(5, 5));
		panel1.add(algebraCalc, BorderLayout.CENTER);
		tabbedPane.addTab("Tab 1", panel1);
		tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
		//Add more of the above code block for more tabs
		
		
		
		add(tabbedPane);
		
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
 
	}
	

	
	
}
	


Thanks for that. I tried it out, did some editing, here is where I got with it:
import calculator.*;

import java.awt.event.KeyEvent;
import javax.swing.*;
import java.awt.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * JTabbedPane class.  All JPanels are brought together here and placed on a JTabbedPane
 * 
 * 
 * @author 
 * @version 1.00 2009/3/23
 */
public class TabbedView extends JPanel { 

	private JTabbedPane tabbedPane;
	private singleVariableCalcPackage algebraCalc = new singleVariableCalcPackage();
	
   /**
	* Class Constructor which sets up the JPanel and calls createGUI method.
	*/
	public TabbedView() {
		super(new GridLayout(1, 1));
		
		setLocation(new Point(0, 0));  
		setVisible(true);			 
		setSize(600, 610);			
		tabbedPane = new JTabbedPane();
		mainPanel();
	}

   /**
	* Sets up each JPanel to be placed on appropiate Tab
	*/
	private void mainPanel(){
		
		JComponent panel1 = new JPanel();
		   panel1.setBackground(new Color (31, 89, 99));
		panel1.setLayout(new BorderLayout(5, 5));
		panel1.add(algebraCalc, BorderLayout.CENTER);
		tabbedPane.addTab("Tab 1", panel1);
		tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
		//Add more of the above code block for more tabs
		
		
		
		add(tabbedPane);
		
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
 
	}
	

	
	
}


That is what I got. It compiles with no errors, but then I examined the code more in depth... there is no place where the actual GUI is set up and shown, is there? I mean, there is, but there is no frame that is set up....

And for some reason, I saved over my old tab file that successfully displayed a set of tabs for this one....

Anyway, back on topic - when I go to run the perfectly compiled .java file, I get this error:

Exception in thread "main" java.lang.NoSuchMethodError: main

I realized there is no main method in this. Is this because this .class file is supposed to be called by yet another class?
Was This Post Helpful? 0
  • +
  • -

#9 wolfman29   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 24-June 09

Re: Need Help Running a Program from Another Program

Posted 29 June 2009 - 10:47 AM

SUCCESS!!!

Finally succeeded, actually, with a lot simpler of a code :P
//imports the calculator functions' source files
import calculator.*;

import javax.swing.*;
import java.awt.*;

public class universalCalculator extends JPanel {

	public static void main(String[] args){

		//Add new objects to add to calculator		

		singleVariableCalcPackage singleVariableAlgebraCalc = new singleVariableCalcPackage();

		//Setting up frame
		JFrame calculator = new JFrame("Universal Calculator");
		calculator.setSize(600,400);
		  calculator.setVisible(true);
 		calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 		
		//Setting up the tabs
		JTabbedPane calcTabs = new JTabbedPane();
 		calculator.add(calcTabs, BorderLayout.CENTER);

 		//Add the functions into tabs
		calcTabs.add("Single Variable Equation", singleVariableAlgebraCalc);

	}
 		
}


This works PERFECTLY!

YES!!!
Was This Post Helpful? 0
  • +
  • -

#10 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Need Help Running a Program from Another Program

Posted 29 June 2009 - 11:14 AM

Thats why i said have your new class extend a JPanel or JFrame. You always need a top level container, like a JFrame, when working with gui's. If you just want your program to enter the system straight into your tabbed pane, just add it to a JFrame like you have done. Even though your code is simpler at the moment, you will need to grab some stuff from mine if you want more than one tab e.g setMnemonicAt, so that you can actually change the tab.

This post has been edited by nick2price: 29 June 2009 - 11:15 AM

Was This Post Helpful? 0
  • +
  • -

#11 wolfman29   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 24-June 09

Re: Need Help Running a Program from Another Program

Posted 29 June 2009 - 12:43 PM

Interestingly enough, I tested it out with another GUI that I just stuck in a second tab, and it worked quite nicely.

Now to make another thread because I am having another problem on another program (double variable solver).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1