AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Calling a .bat file from a java program******

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Language Programming
View previous topic :: View next topic  
Author Message
vysh
Guest





PostPosted: Fri May 18, 2007 7:12 am    Post subject: Calling a .bat file from a java program****** Reply with quote



hi,
Can anyone provide me some hints on calling a "*.bat" file from a
java program.I have a .bat file, in which I am calling another third
party java class which is there in a jar file.
Let this be my bat file.
A.bat
set classpath="%CLASSPATH%";c:\something
c:\jdk\jre\bin\java myClassName parameter 1 parameter 2 parameter3

I need to call this .bat file from another java program and provide
the three parameters at run time.
I hope you got my issue.

any ideas?
thanks,
vysh.
Back to top
vysh
Guest





PostPosted: Fri May 18, 2007 7:12 am    Post subject: Re: Calling a .bat file from a java program****** Reply with quote



On May 18, 11:46 am, "Andrew Thompson" <u32984@uwe> wrote:
Quote:
vysh wrote:

..

Can anyone provide me some hints on calling a "*.bat" file from a
java program.I have a .bat file, in which I am calling another third
party java class which is there in a jar file.

Why not call the class directly from Java code?
Simply ensure the class is on the classpath of
the first application, then..

I need to call this ... from another java program and provide
the three parameters at run time.

.the class can be loaded by importing it and
creating an instance.

Other strategies (assuming the second jar is not on
the original app's classpath) are to use an URLClassLoader,
pointing to the new Jar.

If all else fails (class not in Jar/in default package/
not known till after start-up), you might need to
'stoop' to using reflection - in order to load it.

any ideas?

Capitalise the first letter of every sentence and
question, and also exclude '*' characters from
the subject line, as they might be mistaken for
(edited) swear words.

--
Andrew Thompsonhttp://www.athompson.info/andrew/

Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200705/1

In that case how to pass the parameters?
import cm.xx.cc.vv.MyClass
.....
......
MyClass x=new MyClass()..//here where to pass my parameters?
Back to top
Andrew Thompson
Guest





PostPosted: Fri May 18, 2007 7:12 am    Post subject: Re: Calling a .bat file from a java program****** Reply with quote



vysh wrote:
...
Quote:
Can anyone provide me some hints on calling a "*.bat" file from a
java program.I have a .bat file, in which I am calling another third
party java class which is there in a jar file.

Why not call the class directly from Java code?
Simply ensure the class is on the classpath of
the first application, then..

Quote:
I need to call this ... from another java program and provide
the three parameters at run time.

..the class can be loaded by importing it and
creating an instance.

Other strategies (assuming the second jar is not on
the original app's classpath) are to use an URLClassLoader,
pointing to the new Jar.

If all else fails (class not in Jar/in default package/
not known till after start-up), you might need to
'stoop' to using reflection - in order to load it.

Quote:
any ideas?

Capitalise the first letter of every sentence and
question, and also exclude '*' characters from
the subject line, as they might be mistaken for
(edited) swear words.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200705/1
Back to top
vysh
Guest





PostPosted: Mon May 21, 2007 7:11 am    Post subject: Re: Calling a .bat file from a java program****** Reply with quote

On May 20, 8:36 pm, squirrel <qianli...@gmail.com> wrote:
Quote:
On May 18, 5:02 pm, "Andrew Thompson" <u32984@uwe> wrote:



vysh wrote:

..

Can anyone provide me some hints on calling a "*.bat" file from a
java program.

I think it is preferable to avoid the .bat file altogether,
but keep forgetting to mention that in addition to the
strategies I have already outlined, you might also use
one of the java.lang.Runtime.exec() method variants to
invoke the JVM (passing the arguments as it is invoked),
.or call the bat file.

--
Andrew Thompsonhttp://www.athompson.info/andrew/

Message posted viahttp://www.javakb.com

And if the third application has output on default stream, in java is
System.out, you need to collect them, it is necessary to make the
third application running correctly.

It does!.actually the end out put is a .text file.
thanks,
vysh
Back to top
vysh
Guest





PostPosted: Mon May 21, 2007 7:11 am    Post subject: Re: Calling a .bat file from a java program****** Reply with quote

On May 18, 5:29 pm, Lew <l...@nospam.lewscanon.com> wrote:
Quote:
vysh wrote:

(run class directly from Java code)
In that case how to pass the parameters?
import cm.xx.cc.vv.MyClass
....
.....
MyClass x=new MyClass()..//here where to pass my parameters?

This is very basic Java knowledge.

Read these, and the rest of the tutorial:
http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html

--
Lew

hi lew,
I know it is something very basic.But I have told before that i dont
have the source code for that class.The only thing I know is, if I
call the class and give a couple of parameters(only as command line
parameters) i will get some specific result.I dont know any of the
methods inside dealing with that class.any ideas?
thanks,
vysh
Back to top
vysh
Guest





PostPosted: Mon May 21, 2007 7:11 am    Post subject: Re: Calling a .bat file from a java program****** Reply with quote

On May 18, 2:02 pm, "Andrew Thompson" <u32984@uwe> wrote:
Quote:
vysh wrote:

..

Can anyone provide me some hints on calling a "*.bat" file from a
java program.

I think it is preferable to avoid the .bat file altogether,
but keep forgetting to mention that in addition to the
strategies I have already outlined, you might also use
one of the java.lang.Runtime.exec() method variants to
invoke the JVM (passing the arguments as it is invoked),
.or call the bat file.

yes!.But i have tried to invoke a test.bat file using the
Runtime.getRuntime.exec() method.It dosen't work!.Here is my code.
String str=s[0];
String batfile="test.bat";
String cmd="./"+batfile+" "+str;
try
{
Process p=Runtime.getRuntime().exec(cmd);
p.waitFor();
}catch(Exception e){
System.out.println(e.getMessage());

Quote:

--
Andrew Thompsonhttp://www.athompson.info/andrew/

Message posted viahttp://www.javakb.com
Back to top
vysh
Guest





PostPosted: Mon May 21, 2007 7:11 am    Post subject: Re: Calling a .bat file from a java program****** Reply with quote

On May 18, 1:52 pm, "Andrew Thompson" <u32984@uwe> wrote:
Quote:
vysh wrote:

(run class directly from Java code)

In that case how to pass the parameters?
import cm.xx.cc.vv.MyClass
....
.....
MyClass x=new MyClass()..//here where to pass my parameters?

Do you have the source to cm.xx.cc.vv.MyClass ?

You guessed it right.I dont have the source code!
The only thing I know is that I can pass a couple of paramters as
command line to that class.Thats it!.So only i have gone for the bath
file idea.

any views?
Back to top
vysh
Guest





PostPosted: Mon May 21, 2007 7:11 am    Post subject: Re: Calling a .bat file from a java program****** Reply with quote

On May 18, 5:31 pm, Patricia Shanahan <p...@acm.org> wrote:
Quote:
vysh wrote:

...

In that case how to pass the parameters?
import cm.xx.cc.vv.MyClass
....
.....
MyClass x=new MyClass()..//here where to pass my parameters?

How about this? If you run a Jar, that does not immediately create a
class instance. It calls the main method, as a static method, with an
array of String as parameter:

MyClass.main( new String[]{param1,param2} );

However, whether this strategy works depends on the design of the
package structures. Any overlap between the Jar and the calling program,
such as two classes with the same name without package declarations,
could cause confusion.

Patricia


Its a good idea patricia.But still there is a huddle.In my application
I am not using any log's.I mean something like log4j, and this
particular class uses log4j.The requirement for the class is that,it
should be called as-----java -Dlog4j.configuration=filename par1 par2.
So if i am calling the main as you suggested,I wont be able to
initialize this log file.So the only way through is a batch file.

any ideas?
thanks for your comments,
vysh
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Language Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.