Running an exe file from java
Hi , can anyone tell me how to run an exe file from my java program. the exe is in the same directory as my java files . When i click on a button i want to put something in the actionPerformed() method which will launch the exe file - prog.exe
Hope someone can help me.
Thanks Jim
Hi !
Look at the RunTime class...
Similar Messages
-
Hi all !!!
Is it possible to run a *.exe file from java.....
if yes how??
if no why not??
regards
adYes. It's possible. One of the Runtime.exec() methods are probably what you want:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html
You can't instantiate a Runtime object, so you'll need to call it up by:
Runtime.getRuntime()
For example, if you want to run the Calculator in Windows, try:
Runtime.getRuntime().exec("calc"); -
I wants to call .Exe file from Java Programme
I wants to call .Exe file from Java programme. Please give answer with example. This very urgent. Help me
hi
u can use Runtime.exec() method in java.lang package
to execute exe files
regards
pnp -
Running .EXE file from Java
Do you know, how run an .exe file, for example Internet Explorer or other program, from Java application?
It's very important for me.Too run external program and pass string arguments see class java doc
of java.lang.Runtime method exec . You see online or download or
rename src.jar to s.zip EXTRACT and see
exec
public Process exec(String[] cmdarray,
String[] envp)
throws IOException
Executes the specified command and arguments in a separate process with the
specified environment.
This is a convenience method. An invocation of the form exec(cmdarray, envp)
behaves in exactly the same way as the invocation exec(cmdarray, envp,
null).
Parameters:
cmdarray - array containing the command to call and its arguments.
envp - array of strings, each element of which has environment variable
settings in the format name=value, or null if the subprocess should inherit
the environment of the current process.
Returns:
A new Process object for managing the subprocess
Throws:
SecurityException - If a security manager exists and its checkExec method
doesn't allow creation of the subprocess
IOException - If an I/O error occurs
NullPointerException - If cmdarray is null, or one of the elements of
cmdarray is null, or one of the elements of envp is null
IndexOutOfBoundsException - If cmdarray is an empty array (has length 0)
See Also:
ProcessBuilder
getRuntime
public static Runtime getRuntime()
Returns the runtime object associated with the current Java application.
Most of the methods of class Runtime are instance methods and must be
invoked with respect to the current runtime object.
Returns:
the Runtime object associated with the current Java application. -
Running exe files from java applications
Hello All,
Is it possible to run executable files from java applications?
I need to run an exe file on the client from the server machine, the exe could reside on either the server or any other machine on the LAN. Is it possible to specify the path of where the exe resides, and run it on a client machine?HI,
I tried to launch a MS Word application using runtime.exec but it gives me some problem
The foll. code to launch a txt file using notepad works.
Runtime rt = Runtime.getRuntime();
String[] callAndArgs = {"notepad.exe","C:\\coo7\\wizard.txt"};
Process child = rt.exec(callAndArgs);
However, oif I try to launch a MS Word application, it asks for the entire path of WINWORD.exe, (unlike just specifying notepad.exe as the first argument in String[] callAndArgs) and this can vary from one machine to another.. how do I get around this?
The foll. code snippet works but the complete path of where WINWORD.exe might be installed on any machine, is not fixed:-(
Runtime rt = Runtime.getRuntime();
String[] callAndArgs = {"C:\\Program Files\\Office\\Office10\\WINWORD.exe","C:\\coo7\\wizard.doc"};
Process child = rt.exec(callAndArgs);
Any idea/suggestions pls.. -
How do I run an exe file from a java program?
I need to run an exe program from within a java program.
what do I need to do this?
thanksSee java.lang.Runtime.exec()
-
Running a jar file from java code
Hi!
Im trying to run a jar file from my code.
I've tried Classloader, but that doesnt work because it doesnt find the images (also embedded in the 2nd jar file).
WHat I would like to do is actually RUN the 2nd jar file from the first jar file. There must be a way to do this right?
any ideas?ok, I found some wonderful code (see below) that will try to start the jar. But it doesn't. What it does is produce the following error when my application runs...
So it's not finding the images in the jar file that I am trying to run? Strange. I checked the URL that sending, but it seems ok....
I think I will check the url again to make sure......
any ideas?
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
the code....
/* From http://java.sun.com/docs/books/tutorial/index.html */
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.Attributes;
* Runs a jar application from any url. Usage is 'java JarRunner url [args..]'
* where url is the url of the jar file and args is optional arguments to be
* passed to the application's main method.
public class JarRunner {
public static void main(String[] args) {
URL url = null;
try {
url = new URL(args[0]);//"VideoTagger.jar");
} catch (MalformedURLException e) {
System.out.println("Invalid URL: ");
// Create the class loader for the application jar file
JarClassLoader cl = new JarClassLoader(url);
// Get the application's main class name
String name = null;
try {
name = cl.getMainClassName();
} catch (IOException e) {
System.err.println("I/O error while loading JAR file:");
e.printStackTrace();
System.exit(1);
if (name == null) {
fatal("Specified jar file does not contain a 'Main-Class'"
+ " manifest attribute");
// Get arguments for the application
String[] newArgs = new String[args.length - 1];
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
// Invoke application's main class
try {
cl.invokeClass(name, newArgs);
} catch (ClassNotFoundException e) {
fatal("Class not found: " + name);
} catch (NoSuchMethodException e) {
fatal("Class does not define a 'main' method: " + name);
} catch (InvocationTargetException e) {
e.getTargetException().printStackTrace();
System.exit(1);
private static void fatal(String s) {
System.err.println(s);
System.exit(1);
* A class loader for loading jar files, both local and remote.
class JarClassLoader extends URLClassLoader {
private URL url;
* Creates a new JarClassLoader for the specified url.
* @param url
* the url of the jar file
public JarClassLoader(URL url) {
super(new URL[] { url });
this.url = url;
* Returns the name of the jar file main class, or null if no "Main-Class"
* manifest attributes was defined.
public String getMainClassName() throws IOException {
URL u = new URL("jar", "", url + "!/");
JarURLConnection uc = (JarURLConnection) u.openConnection();
Attributes attr = uc.getMainAttributes();
return attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;
* Invokes the application in this jar file given the name of the main class
* and an array of arguments. The class must define a static method "main"
* which takes an array of String arguemtns and is of return type "void".
* @param name
* the name of the main class
* @param args
* the arguments for the application
* @exception ClassNotFoundException
* if the specified class could not be found
* @exception NoSuchMethodException
* if the specified class does not contain a "main" method
* @exception InvocationTargetException
* if the application raised an exception
public void invokeClass(String name, String[] args)
throws ClassNotFoundException, NoSuchMethodException,
InvocationTargetException {
Class c = loadClass(name);
Method m = c.getMethod("main", new Class[] { args.getClass() });
m.setAccessible(true);
int mods = m.getModifiers();
if (m.getReturnType() != void.class || !Modifier.isStatic(mods)
|| !Modifier.isPublic(mods)) {
throw new NoSuchMethodException("main");
try {
m.invoke(null, new Object[] { args });
} catch (IllegalAccessException e) {
// This should not happen, as we have disabled access checks
} -
heyy
can anybody please help me out in calling an exe file using java??
also i would like to pass parameters tooo.
i have been trying the following runtime code
but found no success
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("make.exe 12");
proc.waitFor();
int exitVal = proc.exitValue();
Does anybody havea better soultion???
Thanks for your time
RachitRead this article, please http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
-
Running exe files from java progam
hi friends
I just want to know how can i run a .bat or .exe file in windows from a java program...
also i would like to know if i can check if mysql is installed in a windows computer from my java program
Thanks
Mustafasmshiraz wrote:
thanks Jos
now i am able to execute the .bat file.....from my java program
Can you also please tell me how can i check from my java program how if a particular application is installed in my windows computer??The exec() call will more than likely fail if the application is not present; again, read the API documentation.
kind regards,
Jos -
Problem in Running EXE file from Java
Hello Friends,
I am calling an EXE program from my JSP page using Runtime.exec() method. The exe file simply reads a file and writes the content into another file (like copying a file). Now if I am providing fully qualified path to both source and destination files, then it runs absolutely fine. However, if i don't mention file path (only fine name is there), it creates output file in System32 folder and the resultant file doesn't contain any data.
I am providing the source for the EXE file.
==============
Source 1 (with fully qualified path name)
#include <stdio.h>
#include <fstream.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
void write_temp_file1(char *);
void main()
char * myTempPath=NULL;
myTempPath=(char *)calloc(100,sizeof(char));
strcpy(myTempPath,"D:\\temp\\tempFile.txt");
write_temp_file1(myTempPath);
myTempPath=NULL;
void write_temp_file1(char * fileName)
// first read the file and write another file with same content
char * line1=NULL;
char * line2=NULL;
char * line3=NULL;
line1=(char *)calloc(100,sizeof(char));
line2=(char *)calloc(100,sizeof(char));
line3=(char *)calloc(100,sizeof(char));
ifstream inf;
inf.open(fileName,ios::nocreate);
inf>>line1;
inf>>line2;
inf>>line3;
inf.close();
ofstream outf;
strcat(fileName, "1");
outf.open(fileName);
outf<<line1<<endl;
outf<<line2<<endl;
outf<<line3<<endl;
outf.close ();
================================
Scenario 2 (with file name only)
#include <stdio.h>
#include <fstream.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
void write_temp_file1(char *);
void main()
char * myTempPath=NULL;
myTempPath=(char *)calloc(100,sizeof(char));
strcpy(myTempPath,"my_temp_from_exe.txt");
write_temp_file1(myTempPath);
myTempPath=NULL;
void write_temp_file1(char * fileName)
// first read the file and write another file with same content
char * line1=NULL;
char * line2=NULL;
char * line3=NULL;
line1=(char *)calloc(100,sizeof(char));
line2=(char *)calloc(100,sizeof(char));
line3=(char *)calloc(100,sizeof(char));
ifstream inf;
inf.open(fileName,ios::nocreate);
inf>>line1;
inf>>line2;
inf>>line3;
inf.close();
ofstream outf;
strcat(fileName, "1");
outf.open(fileName);
outf<<line1<<endl;
outf<<line2<<endl;
outf<<line3<<endl;
outf.close ();
==========================
Can anyone figure out what could be the problem ? I think there is a problem with Library Path or sort of something like that.
Please help me.
-- Niranjanproviding source code for the EXE program doesn't tell your problem.
You have to be more specific. What is your problem? -
Unable to run an exe file from a Servlet
I am not able to run a Servlet that calls an exe file using the following code. trial.exe is was built from a C program and it is simply a program that creates a text file with some content. I am using Tomcat 6.0 and JDK 1.5 on a Windows XP.
{color:#000080}try
Runtime rt=Runtime.getRuntime();
String[] command = {"C://Tomcat 6.0//webapps//ROOT//WEB-INF//classes//trial.exe"};
{color}
{color:#000080}
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
catch(Throwable t)
// System.out.print(t.getMessage());
}{color}
Before running the code on Tomcat I've compiled the code above in a simple java programe and it executed without any problem.
Then I compiled the code within a servlet. No luck. Then I treid the following options:
Executed the Tomcat service under the context of a user by specifying a user(Log On option) for the Tomcat service.
Then with Local System Account, selecting “Allow service to interact with desktop”.
this time also, no luck. Can somebody help me on this please ?
BJJFor clarity I am rewriting the last sentences here:
Then I compiled the above code within a servlet successfully and invoked the servlet from a browser with and without the following settings of Tomcat 6.0.
1. Executed the Tomcat service under the context of a user by specifying a user (Log On option) for the Tomcat service.
2. Then with Local System Account, selecting �Allow service to interact with desktop�.
I was not succeeded with any of the methods mentioned above. -
Calling a function in an EXE file from Java Program
Hi Im having a function which is written in c program.i need to call that function from my java program, if i create a shared library (DLL) for my C code then it works but my requirement is i dont want to create that DLL , like in it would be an executable and my java code should access that function in that C program
I understand the usage od a DLL but the thing is if i convert the exe to a DLL
the server doesnt start at all so what i need is that i dont want to change
that .EXE into a .DLL,let it be an executable. that executable is in running mode
and through my java program i need to call a function in that EXE file.
Is ther any way to do it?Nope, but you have another problem: why can't you separate your server program
into a .dll part and a startup part? Both, when properly linked against each other
should give you an executable file.
kind regards,
Jos -
I want to run a EXE from java code. I did it using Runtime.getRuntime.exec("")...
but is there a way to prevent the EXE from modifying the files already on the system...I mean the EXE is from a untrusted source......so it may do some malicious activities...is there a way I can stop the EXE from modifying the contents of other files......
Thanks.The people here are volunteers. They post answers for whatever reasons.
but is there a way to prevent the EXE from modifying the files already on the system...That question has nothing to do with java. Nor really any programming language for that matter. It is an OS question and one that is specific to the OS.
When an executable is called in java the OS creates another application. Just as if you ran it yourself.
So the question is whether your OS allows that.
Given that you said "exe" I presume you mean windows. And that limits your answer to windows users. So you would need a windows user created, which only has read permissions on the system. Then you figure out a windows command that lets you run an application as another user. I am not sure that such an app comes with windows but it if doesn't it can be coded - in C/C++ not java. -
Hi,
Do you know a way to run the "startup.exe" Tomcat file from jsp?
I have to start and stop a server remotely.
Maxpublic Process pFiglio;
Runtime rt = Runtime.getRuntime();
pFiglio = rt.exec("c:\\Tomcat\\startup.exe ");
then you can have
pFiglio.waitFor();
iEsito = pFiglio.exitValue();
pFiglio.destroy();
Look at specification:
http://java.sun.com/j2se/1.5.0/docs/api/
Look class "Runtime" and "Process".
Ema -
Running the .class file from java code
I'm doing a kind of providing service like compiling and running Java code on server side and giving output to the end user.
Please suggest me an approach with code to run .class file from the Java code.
import java.io.*;
public class demo {
public static void main(String args[]) throws IOException, InterruptedException {
int result;
try {
System.out.println("command output:");
Process proc = Runtime.getRuntime().exec("java -cp . demoh");
InputStream in = proc.getInputStream();
result = proc.waitFor();
BufferedInputStream buffer = new BufferedInputStream(proc.getInputStream());
BufferedReader commandOutput = new BufferedReader(new InputStreamReader(buffer));
String line = null;
System.out.print(commandOutput);
try {
while ((line = commandOutput.readLine()) != null) {
System.out.print(line);
System.out.println("command output: " + line);
}//end while
commandOutput.close();
} catch (IOException e) {
//log and/or handle it
}//end catc
} catch (IOException e) {
System.err.println("IOException raised: " + e.getMessage());
}What happened when you tried what you have there?
Maybe you are looking for
-
I want only one Apple acct....please!
I have 4 Apple related accounts: 2 "apple.com", 1 "daw.apple.com", 1 "secure1.store.apple.com" How can I have only one acct. for Apple, iTunes, Cloud, etc.?
-
I have a MacBook that needs its internal hard drive's Macintosh HD partition repaired, as the boot screen freezes on startup. I am running a separate, working mac OS from an external hard drive in order to run disk utility and repair the MacBook's i
-
I have a layered PDF and need to create a slideshow of each layer. By the first page I can toggle layers on or off on each new page, but Acrobat insists on building thumbnails pages and it really slows the process down. How do I tell Acrobat Pro 9 I
-
Migration Assistant - from non functioning imac to new imac?
My daughter's G5 imac has a bad graphics card (per apple store genius). Logic board is apparently OK but computer can't be used. She has decided to buy new a imac. Her concern is all of the data on her old imac's hard drive which she wants to transfe
-
Platform migration vi storage replication?
We have a 10.2.0.4 database running on HPUX platform which uses EMC for it's storage. We want to migrate the database to an AIX server. Is it possible to just clone the luns where the datafiles live using the EMC utilities and then bring up a databas