Solved

Call a local BATCH Job or Run MS ISQL

Posted on 1998-07-27
6
478 Views
Last Modified: 2008-02-26
I would like java to 'call' or 'run' a command on the same server as java is running.  The command must run an ISQL script to work on a local SQL Server and/or batch file.  How can I do it?
0
Comment
Question by:snydero
  • 3
  • 3
6 Comments
 
LVL 1

Accepted Solution

by:
dryang earned 100 total points
ID: 1228389
Thread it! spawn a thread to run the script using the exec command. but watch out for synchronization mistakes. any comments please post again
0
 
LVL 1

Author Comment

by:snydero
ID: 1228390
I was hoping for an example of a line of code.
0
 
LVL 1

Expert Comment

by:dryang
ID: 1228391
here goes.....

1) create a thread, either by extending class 'Thread' or implementing 'Runnable' in your self created class. If you want to extend 'Thread' make sure you have 'public void run()' as part of your code. The JVM will run this method automatically after the class has been called

eg.
public class myclass extends Thread
{
    myclass()    //constructor
    {}

    public void run()  //you MUST have this method (overrides run method of Thread)
    {  //does something useful
     }
}

In your calling program...

myclass mc = new myclass();
mc.run();

and the thread runs. Its the same for implementing 'Runnable'

Check the JDK documentation for specific details and synchronization issues. (check out the key word 'synchronised')


2) After you have created your code for threading, use the 'exec' command to exececute any batch jobs or scripts.(under java.lang.Runtime)

eg.
Runtime.exec("autoexec.bat");

There are various forms of exec and I reccommend that you dig around the documentation for details. Its not easy to decide which form is the best and you should take some time to look over your design specs.


note: In UNIX we use a similar method called 'fork and exec' where a seperate process is created (forked) and a command run (exec). This is where JAVA takes the idea from.

post any futhur questions ok?
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 1

Author Comment

by:snydero
ID: 1228392
Here is my code...
class HelloWorld
{      public static void main(String args[])
      {      System.out.println("Hello World!");
            rclass rc = new rclass();
            Runtime.exec("jtest.bat");
      }
}

class rclass extends Thread
{      rclass()
      {}
      public void run()
      {      System.out.println("In run now");
      }
}

The error I get is...
helloWorld.java:5: Can't make static reference to method java.lang.Process exec(java.lang.String) in class java.lang.Runtime.
            Runtime.exec("jtest.bat");
                        ^
1 error

0
 
LVL 1

Expert Comment

by:dryang
ID: 1228393
hi Syndero

Oops, Syntax error on my part.

should instantiate the Runtime object first...

Runtime rt = new Runtime();
rt.exec("jtest.bat");
0
 
LVL 1

Author Comment

by:snydero
ID: 1228394
I've tried every way to get your code example to work.  It DOESN'T!
Can you provide a complete listing please.
Again, I wish to run a batch file (jtest.bat) from a java program.
Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java stored proc example 9 44
Error in @AspectJ Based AOP with Spring 2 18
eclipse buid path vs tomcat lib path 10 34
difference between sorce folder and folder in eclipise 3 28
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question