File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Run a Java Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Run a Java Program " Watch "Run a Java Program " New topic
Author

Run a Java Program

Justin Char
Ranch Hand

Joined: Mar 05, 2007
Posts: 76
I would like to run a java program lets say justin.java

Problem is:

I want to create a jar file.

Click on that jar file

Have a joption pane come up with three buttons

First button runs the program
the other two give the results.

I can do all of this except get the program to initially run after running my main program runs. I can right click on justin.java (run app) and it runs fine. I just don't know how to select a program with a JButton and cause the program to run. So I can select the next button and see results.

I can select second JButton and have it show me the results. I would think just requesting that program would cause it to execute but I guess not.

Any help is really appreciated. Below is a couple things I have tried.
[code]
if (Jmoney.equals("MMMMMM"))
{

new Justin ();

[code/]
[CODE]
// try {
//theProcess = Runtime.getRuntime().exec("C:\\EclipseProjects\\PM\\Justin.java");
//} catch (IOException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
//}
[code/]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The first thing you want to do is figure out what you mean by 'running another application'. Do you want to run the application in another JVM (using Runtime) or in the same JVM? How do you get the results from the other application?

Second thing is to understand exactly what runs when you run a java program. You don't execute the .java file, which is just source code. The .class file is what actually runs. But it isn't an executable, so you can't just run it by itself, you need to run the Java Virtual Machine (java.exe) passing in the class you need to run (and any classpath information - like the JAR file the class is in).

So, things to learn first:
1) Execute a Java application from the command line
2) Execute a Java application inside a JAR file from the command line
3) How to use Runtime.exec() (for which this important article is an imperative read.


Steve
Justin Char
Ranch Hand

Joined: Mar 05, 2007
Posts: 76
I read the entire article. I feel more confused. But I did learn
I need to have a proc.waitFor(); or exitValue();

It seems I need to use

You were saying I need to run the class but I don't think the class will run by just selecting it. I am missing a little piece. Or do I create a exe somehow with the class?

I am only trying to run a java class inside one project and one package.
There are 6 classes all together that run off the mainprogram. Using a Joption pane with jButtons grabbing files and viewing data.

I want to create a jar and put it on a users pc so they can run program
and get the results without seeing any code or having to run it manually. I get do everything except get the Runtime.exec to work.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Wait, stop, using Runtime was step 3.

Did you open up a DOS window and try running your java application from there? If you can't, then you should read this: Java Tutorial: Getting Started for Windows Command Line.
[ October 30, 2008: Message edited by: Steve Luke ]
Justin Char
Ranch Hand

Joined: Mar 05, 2007
Posts: 76
I read that article also.

I cannot run my programs from the dos prompt

gives me an error they are not executable.

I think we are off the mark with this.

I use eclipse I have a Java_Home path set. In Enviromental Variables. I run bat files all the time.

I point to my program going through the CMD Prompt and it doesn't execute.

Unless you wanted me to do something else.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You are actually shooting yourself in the foot by relying on Eclipse, or any IDE. You need to understand how Java works, first. Once the basics get boring then you go to an IDE to do more stuff faster.

Show me the exact command you use on the dos prompt, and copy the exact error message you get. We can then fix the problem.
Justin Char
Ranch Hand

Joined: Mar 05, 2007
Posts: 76
I think I made a mistake in what I was asking after a lot of thought.

I want to run a java class not a java program off a JButton click.

I can click the JButtons and make then bring me file outputs, images ect.

I want to click the JButton and have it run a class. I want to run the main class and then be able to run other classes from a JButton click.

Thank You in advance.
Justin Char
Ranch Hand

Joined: Mar 05, 2007
Posts: 76
compile in cmd prompt.

c:\javac Justin.java

c:\java Justin

Hello World!

I ran a simple Hello World from the cmd prompt.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by Justin Charpenter:
I think I made a mistake in what I was asking after a lot of thought.

I want to run a java class not a java program off a JButton click.

I can click the JButtons and make then bring me file outputs, images ect.

I want to click the JButton and have it run a class. I want to run the main class and then be able to run other classes from a JButton click.

Thank You in advance.


So let's go back to my original question:
"The first thing you want to do is figure out what you mean by 'running another class (edit)'." What constitutes 'running' for the other class?
Justin Char
Ranch Hand

Joined: Mar 05, 2007
Posts: 76
Running would be the following.

I have a class called M

When I compile the code and run it I get a 07E count text file
and a 07A count text file and a 07E/07A transaction file

I have a swing gui with three JButtons on it.
I can select the three buttons and get each text file and transaction file.

My problem is I would like to have a gui with one JButton on it click the button and run the class M.

I also have a jar file of the class I just don't know how to execute a jar file from a button click either.

I must not be asking the right questions. Sorry if I am not detailed enough.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Just think out the solution. You can run an application from the command prompt, right?

What does running the application from the command prompt actually do? Calls the public static void main(String[] args) method, correct?

So if you wanted to 'run' this application from a button click, what can you do? You have two options (not including runtime, stay away from that for now). What do you think they are?
Justin Char
Ranch Hand

Joined: Mar 05, 2007
Posts: 76
I first thought I could just use That didn't work.

The above attributes are in my Main program.

Then I thought I could just have it run in the Main Method.

I changed parent from a small p to Parent Capitol and it works now.

I could have changed something else also. I want to understand why it is working though and I don't.

Should
As long as I am showing Parent should this run the class or is something else causing it to execute.

Thank You
Justin
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

None of that code makes much sense to me. It doesn't appear to be java except for the single line where you do jac = new JAC(this); (and then it seems like the code is upside down:

instead of



doesn't make much sense to me unless MainProgram is a class, and Parent would be a variable name. If so, then
1) remember that Java is case sensitive so parent and Parent are different
2) try to follow the coding conventions Sun defined: Naiming Conventions. They will make things easier to read (especially when someone else reads your code) to be able to quickly identify what is a variable, what is a class, etc...

Unfortunately, I don't really understand your question, your code, or your problem. Sorry.
Justin Char
Ranch Hand

Joined: Mar 05, 2007
Posts: 76
Thank You for all your help...

Especially Steve you never gave up...

I found out what the problem was...

I had two main's in my group of classes...

I thought I needed a main in a non-main class I didn't realize that wouldn't work being I have a main class with a main that all the other classes would need the main removed. I was combining 5 classes one had a main in it which needed to be removed but I didn't realize that was the problem.

Learning experience. Thanks again for all the suggestions. I think I was over analyzing the problem. Steve I will work on my Naming Conventions. I am self taught so my code isn't by the book per say.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Wish I coulda helped more... Ahh well, as long as you figured it out... glad to see you got it working. Keep up the good work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Run a Java Program