java program with button to run a batch file from windows

Hello,

Im kinda new to java but not other languages.  I need to write a program in java which has an interface from windows and is ran from a windows icon.

the first thing i want to do is have the program execute a batch file called DTASelect.

Ive written jsp's and was able to run lots of commands through

  Process DTA = Runtime.getRuntime().exec(" ");

normally from the dos prompt to run DTASelct u just type DTASelect

I tried the following:

import java.io.*;
import java.lang.*;
import javax.swing.*;
public class hello
{
  public static void main( String[] args ) {
try{
      Process DTA = Runtime.getRuntime().exec("C:\\DTASelect\\DTASelect");   //location of batch file

      Process p = Runtime.getRuntime().exec("dir");
 
      JFrame frame = new JFrame( "HelloJava" ); //program i started with
      JLabel label = new JLabel("Hello, Java!", JLabel.CENTER );  //same
    frame.getContentPane().add( label ); //same
    frame.setSize( 300, 300 );//same
    frame.setVisible( true ); //same
  }
catch(Exception e) {
      System.out.println("no good");
    }



}}

the program just prints out "no good"

Im just starting out here ... i figure once i can get the java program to run DTASelect adding a button and other properties to cause this shouldnt be too difficult.

on a different note when i run java  programs on ms dos with classes they dont seem to like them

i get the error: "exception in thread 'main' java.lang.NoClassDefFoundError: HelloComponent2 - eventho this program was from a book and should work fine - could compile and all
 
bryanbergAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
does the batch file DTASelect has extension like .cmd or .bat if so then change the line from :

     Process DTA = Runtime.getRuntime().exec("C:\\DTASelect\\DTASelect");   //location of batch file

to:

     Process DTA = Runtime.getRuntime().exec("C:\\DTASelect\\DTASelect.bat");   //location of batch file
0
DrWarezzCommented:
Yeah, petmagdy's advice should solve that.
Check out http://www.javaalmanac.com/egs/java.lang/Exec.html for more info on using command via Java.

>"is ran from a windows icon."
Create a JAR file. Check out: http://java.sun.com/docs/books/tutorial/jar/basics/ for more info on how to do that. :) And just ask if you have any trouble.

HTH :)
[r.D]
0
bryanbergAuthor Commented:
hello again

thanks for the response ... the DTASelect command is now working ...

except when we used to run it from the dos prompt the output of the program would be displayed on the screen.  now the dos prompt does not display anything - this does not matter since were gonna make a windows icon to run the program, but we do want the output to be displayed on the frame that is created in the code above

any ideas on obtaining the output from this process would be appreciated,

thanks ! : >

bryan
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

DrWarezzCommented:
Oh -- did you find a solution to:
"i get the error: "exception in thread 'main' java.lang.NoClassDefFoundError: HelloComponent2 - eventho this program was from a book and should work fine - could compile and all"
??

If not, it's most probably because the class isn't in the CLASSPATH variable.
Before compiling and (most importantly) running your class files, type:

set classpath=%classpath%;%cd%

;-)

So, the batch file isn't giving output? But you need to retrieve it, right?

Perhaps try passing all output to a file, like so:

    Process DTA = Runtime.getRuntime().exec("C:\\DTASelect\\DTASelect.bat ^> Filename.txt");   //location of batch file

Then search at: www.javaalmanac.com for "file" to find how to read from a file, and assign the content to a string, which you can then process :)

If no luck, then you may need to pass all the output commands in the actualy batch file to a text file. If you decide to do that, give me a copy of the batch file, and I'll add all the appropriate output commands. :D

gL,
[r.D]
0
bryanbergAuthor Commented:
hmmm ...

i found this ...

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20306549.html?query=process+java+output&clearTAFilter=true

and was able to get the dos prompt screen to output the results by using ..

java.io.InputStream in = DTA.getInputStream();

int r;
while ((r=in.read()) != -1) System.out.write(r);

where DTA is the name of my process

so im thinking that writing the output to a jframe cant be too much more ...

starting to mess with jTextAreas

this stuffs kinda tricky so far ...

the link above might have what i need but when why tried compiling parts i got errors

thanks again,

bryan

0
bryanbergAuthor Commented:
well im on my way ... ive got my program to open up a window and then run the DTASelect when a button is clicked ... its still running from a dos prompt tho ...havent gotten to that part about making a windows icon yet ...

bryan


import java.io.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
 import java.awt.event.*;



public class hello

{
  public static void main( String[] args ) {
try{
      //Process DTA = Runtime.getRuntime().exec("C:\\DTASelect\\DTASelect.bat");  
      //final Process DTA = Runtime.getRuntime().exec("java -cp c:\\DTASelect DTASelect");  
      //Process p = Runtime.getRuntime().exec("dir");

//Process p = Runtime.getRuntime().exec();



       //javax.swing.JTextArea textArea;

      //java.io.InputStream in = DTA.getInputStream();
      //java.io.BufferedReader reader = new java.io.InputStreamReader(in); // wrapping the stream

//String r;
//while ((r=reader.readLine()) != null) // the reading is slightly changed
    //textArea.append(r + "\r\n");

      JFrame frame = new JFrame( "HelloJava" );
      JLabel label = new JLabel("Hello, Java!", JLabel.CENTER );
      JButton button = new JButton("DTA");
      button.setSize( 50, 50 );


frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      final JTextArea area = new JTextArea( );
        area.setText("Howdy\n");

 
button.addActionListener(new ActionListener( ) {
      public void actionPerformed(ActionEvent ae) {
try{
      final Process DTA = Runtime.getRuntime().exec("java -cp c:\\DTASelect DTASelect");
java.io.InputStream in = DTA.getInputStream();
InputStreamReader converter = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(converter);
String r;
while ((r=reader.readLine()) != null) // the reading is slightly changed
area.append(r + "\r\n");

  }
catch(Exception e) {
      System.out.println("no good");
    }


} });




//java.io.BufferedReader reader = new java.io.InputStreamReader(in); // wrapping the stream


//int r;
//while ((r=in.read()) != -1) System.out.write(r);
//while ((r=in.read()) != -1) area.setText(r);

//javax.swing.JTextArea textArea;
//textArea.append("hello");
Container c = frame.getContentPane( );
    c.add( label,BorderLayout.NORTH);
    c.add( area,BorderLayout.CENTER);
    c.add( button,BorderLayout.SOUTH);      
      frame.setSize( 300, 300 );
    frame.setVisible( true );
  }
catch(Exception e) {
      System.out.println("no good");
    }

}}
0
bryanbergAuthor Commented:
Hello again,

well I seem to be having problems with my jar file

I was able to create a jar file using Orely's Learning Java 2nd edition

but then when I added the myManifest.mf file, which looks like this:

Main-Class: hello

and then try to run the jar program I get the error:

"Exception in thread "main" java.lang.NoClassDefFoundError: hello

i changed the classpath to allow java to lookinto the jar file with:

set claspath=%classpath%;%cd%;C:\DTASelect\test_run\DTAj.jar

help ! (ive already tried redoing it)

0
bryanbergAuthor Commented:
well Ive gotten the program to run from the windows icon but not my using the jar method

instead I created a batch file which ran the java program and then made a shortcut to the batch file to be in the same directory as the main class file.

but i still want to know how to get that jar program to work - im guessing ive either done something wrong with either the myManifest.mf file or the classpath directory

after agonizing over it for the last day id really like to figure this out - id imagine itll be helpful in the future

btw any known problems with using the batch file ?

what are the benefits to using the jar file ? - im guessing with a jar file the pc doesnt have to have java installed on it ...

0
DrWarezzCommented:
Hi Bryan,
Sorry about this large delay, I'm here to help though now... :-)

>"set claspath=%classpath%;%cd%;C:\DTASelect\test_run\DTAj.jar"
I hope that was just a typoe will typing into the EE screen.. right? Because you've put: set claspath= ...
Whereas it SHOULD be:  set classpath= ...

>"im guessing with a jar file the pc doesnt have to have java installed on it ..."
Incorrect. To run ANY java program, the machine MUST have JRE (Java Runtime Environment) installed! :-(
Even for JAR files.
If you want to be able to run the file without needing JRE installed, you must use a 'Bytecode to Native Code' compiler. Which turns the JAR file into an EXE. However, you must pay for these (well, good ones). The best FREE one can't compile Java programs that use the SWING package (for the GUI)!!!  So, forget about that! :-)

Anyways, the advantages of a JAR file. Well, it depends what you're using them for, if you're building an applet, they help ALOT, and speed up the download time (for the client).

But also, if your software uses other files, such as images, then you can compile all of these files, and compress them into ONE! :D
Meaning that you only need to send your mate ONE file to use it, and only requires a double-click to run it (assuming he has JRE installed). JAR files are very useful.

>"btw any known problems with using the batch file ?"
erm... Not really. But it's definitely NOT the best method. I'll try and help you to get the JAR file working, but until then, use this method:
Create a shortcut, with the following line:

javaw -cp c:\directory ClassName

So, let's imagine that you have the following:

   c:\directory\ClassName.class
 
Then you'd use:

javaw -cp c:\directory ClassName

As the shortcut. :-)

Then you just double-click the shortcut, and off you go. However, if you're using imagery, you really want to just use a JAR file, you'll find out why if you try the above method with images. ;-)

As for the JAR problem, have you seen here:
http://java.sun.com/docs/books/tutorial/jar/basics/

I think creating and using JAR files is explained pretty well there :-)

Let me know if you have any problems.
Good luck,
[r.D]
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DrWarezzCommented:
8-) Thanks V..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.