Run a EXE 'safely' from java
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.
Similar Messages
-
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 JimHi !
Look at the RunTime class... -
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"); -
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()
-
Hi,
how can i run a unix script from java application. This java application is on windows.
How can i do this.
thanks,Hi,
how can i run a unix script from java application.
This java application is on windows.
So I think it's safe to assume that the target script is on a remote unix server.
Take a look at http://sourceforge.net/projects/sshtools/ -
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.. -
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? -
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 -
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
} -
Closing a running windows XP program from java
Hello,
Im currently making a program in java under sdk 1.4.0_01 and i want to be able to close a program which is already running in windows(Emule).
Is this possible?
I know how to run it using: Process proc = Runtime.getRuntime().exec ("mulePath");
...but i dont know how to close it unless ive already run it from java.
Any help appreciated!
CraigSearch the web for the NT Toolkit. It has utilities that you can use to kill a process on NT type os's.
-
Run dial-up connection from Java Program?
Is there a way to run dial-up connection from a Java Program? It needs to platform independent.
Thanks.
VirumI very much doubt it, at least not platform independent. I had a, oops, heck of a time doing that from Visual Basic, where it is much easier to work with operating-system stuff like that than it is in Java. I finally ended up buying a RAS component to call from my VB program.
-
How can I run a dos command from java on windows 98, 95?
The usage of cmd.exe in the java program to run a dos command on windows 98. 95 doesn't work as the equivalent command is command.exe
But using the command.exe in the java program makes my program to hang.hi,
As u mentioned, u cannot use the cmd.exe in win9x environment as cmd.exe is specific to windows NT, you can use the command.exe without any hitches.
for eg
java.lang.Runtime.getRuntime().exec("start command /K a.bat"); should run the batch file a.bat..
if the problem persists, try posting the snippet of code that you are using.
cheerz
ynkrish -
How to run the perl script from java ?
Hi , I need to run the pearl script from the server and to get the result of the script thro' java using SSH
Is there any 3rd party SSH API in java ?
Please help me out
thanks in advance
karthikThis seems like a very strange thing to want to do.
What is the perl script doing?
Do other programs (not some shell script) access this server-side perl script from a different machine?
If so how do they do it?
What currently triggers the perl script to execute?
What currently handles the output from the perl script?
If you can answer these questions (and understand the answers) you should be able to come up with a different approach to this.
You do not really want to call a remote perl script from a Java program, you want to achieve the effect you think that would have if you could do it.
So find the answers to the above questions, write them on 3" x 5" cards and lay them out on your desk.
What flow of control needs to happen? Which piece of code needs to produce or consume which piece of data?
If that fails, ask you self or who ever is making you do this what are the use-cases?
If you do not know what use-cases are or do not understand them well enough try reading
Use Case Modeling (The Addison-Wesley Object Technology Series) by Kurt Bittner and Ian Spence (Paperback - Aug 30, 2002)
http://www.amazon.com/s/ref=nb_ss_gw/002-7908514-4043267?url=search-alias%3Dstripbooks&field-keywords=use+cases&x=0&y=0
It is a short and easy to read book on the subject and also one of the best.
You MUST understand WHAT you are trying to achieve before you can decide HOW you are going to achieve it.
Maybe you are looking for
-
i have been given an iphone 4s from my cousin ,she has restored the factory settings and i cant get the phone to accept my sim card as i guess it is a different carrier how could i fix the problem or wipe the imei number ? p.s i am computer illiterat
-
MAC Desktop software lack of support
New to this board, but have posted in numerous other boards. When is RIM going release a desktop mgr software that is compatible with MAC? Yes, I know there is Pocketmac, but that application is worth the price that you pay for it...nothing. I h
-
Verizon employees admit their error--then Verizon decimates my credit history, and my life
Hurricane Sandy knocked out Time Warner's high-speed cable internet service in my NYC neighborhood for a few days. I would later learn that Verizon FIOS and DSL were both disputed in other neighborhoods, but in the immediate post-Sandy confusion, Ver
-
Importing multiple files in a sequence
Hi - I am new to this forum - excuse me if this has already discussed eslewhere. I am trying to see if there is a way of importing clips from an excel file which has the folder name, file name and the time stamp from which the video shd play and impo
-
HT1657 can i watch a rented movie where there is no wifi? like on a plane?
Can I watch a rented movie on my IPAD2 where there is no wifi? for example: download a rented movie at home then take it to an airplane and watch it.