Need urgent help. Calling of exe files from java program
This program can execute small .exe files that donot take inputs but doesn't work for exe files that takes input. what could be the problem.
Server code :-
import java.io.*;
import java.net.*;
public class Server1 {
private Player[] players;
private ServerSocket server;
private ExecHelper exh;
String command = null;
String message = null;
public Server1() {
players = new Player[5];
try {
server = new ServerSocket( 12345, 5 );
System.out.println("Server started...");
System.out.println("Waiting for request...");
catch( IOException ioe ) {
ioe.printStackTrace();
System.exit( 1 );
} //end Server constructor
public void execute() {
for( int i = 0; i < players.length; i++ )
try {
players[i] = new Player( server.accept() );
players.start();
catch( IOException ioe ) {
ioe.printStackTrace();
System.exit( 1 );
public static void main( String args[] ) {
Server1 ser = new Server1();
ser.execute();
System.exit( 1 );
private class Player extends Thread {
private Socket connection;
private ObjectOutputStream output;
private ObjectInputStream input;
public Player( Socket socket ) {
connection = socket;
try {
input = new ObjectInputStream( connection.getInputStream());
output = new ObjectOutputStream( connection.getOutputStream());
output.flush();
catch( IOException ioe ) {
ioe.printStackTrace();
System.exit( 1 );
public void run() {
try {
message = "Enter a command:";
output.writeObject( message );
output.flush();
do {
command = ( String ) input.readObject();
String osName = System.getProperty( "os.name" );
String[] cmd = new String[3];
if( osName.equals( "Windows 2000" )) {
cmd[0] = "cmd.exe";
cmd[1] = "/c";
cmd[2] = command;
else if( osName.equals( "Windows NT" ) ) {
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = command ;
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec( cmd );
exh = new ExecHelper( proc, output, input);
} while( !command.equals( "TERMINATE" ) );
catch( Throwable t ) {
t.printStackTrace();
} //end class Player
public class ExecHelper implements Runnable {
private Process process;
private InputStream pErrorStream;
private InputStream pInputStream;
private OutputStream pOutputStream;
private InputStreamReader isr;
private InputStreamReader esr;
private PrintWriter outputWriter;
private ObjectOutputStream out;
private ObjectInputStream in;
private BufferedReader inBuffer;
private BufferedReader errBuffer;
private Thread processThread;
private Thread inReadThread;
private Thread errReadThread;
private Thread outWriteThread;
public ExecHelper( Process p, ObjectOutputStream output, ObjectInputStream input ) {
process = p;
pErrorStream = process.getErrorStream();
pInputStream = process.getInputStream();
pOutputStream = process.getOutputStream();
outputWriter = new PrintWriter( pOutputStream, true );
in = input;
out = output;
processThread = new Thread( this );
inReadThread = new Thread( this );
errReadThread = new Thread( this );
outWriteThread = new Thread( this );
processThread.start();
inReadThread.start();
errReadThread.start();
outWriteThread.start();
public void processEnded( int exitValue ) {
try {
Thread.sleep( 1000 );
catch( InterruptedException ie ) {
ie.printStackTrace();
public void processNewInput( String input ) {
try {
out.writeObject( "\n" + input );
out.flush();
catch( IOException ioe ) {
ioe.printStackTrace();
public void processNewError( String error ) {
try {
out.writeObject( "\n" + error );
out.flush();
catch( IOException ioe ) {
ioe.printStackTrace();
public void println( String output ) {
outputWriter.println( output + "\n" );
public void run() {
if( processThread == Thread.currentThread()) {
try {
processEnded( process.waitFor());
catch( InterruptedException ie ) {
ie.printStackTrace();
else if( inReadThread == Thread.currentThread() ) {
try {
isr = new InputStreamReader( pInputStream );
inBuffer = new BufferedReader( isr );
String line = null;
while(( line = inBuffer.readLine()) != null ) {
processNewInput( line );
catch( IOException ioe ) {
ioe.printStackTrace();
else if( outWriteThread == Thread.currentThread() ) {
try {
String nline = null;
nline = ( String ) in.readObject();
println( nline );
catch( ClassNotFoundException cnfe ) {
// cnfe.printStackTrace();
catch( IOException ioe ) {
ioe.printStackTrace();
else if( errReadThread == Thread.currentThread() ) {
try {
esr = new InputStreamReader( pErrorStream );
errBuffer = new BufferedReader( esr );
String nline = null;
while(( nline = errBuffer.readLine()) != null ) {
processNewError( nline );
catch( IOException ioe ) {
ioe.printStackTrace();
Client code :-
// client.java
import java.io.*;
import java.net.*;
public class Client {
private ObjectOutputStream output;
private ObjectInputStream input;
private String chatServer;
private String message = "";
private Socket client;
public Client( String host ) {
chatServer = host;
private void runClient() {
try {
connectToServer();
getStreams();
processConnection();
catch( EOFException eofe ) {
System.err.println( "Client terminated connection ");
catch( IOException ioe ) {
ioe.printStackTrace();
finally {
closeConnection();
} //end method runClient
private void connectToServer() throws IOException {
System.out.println( "Attempting connection...\n");
client = new Socket( InetAddress.getByName( chatServer ), 12345);
System.out.println( "Connected to : "+ client.getInetAddress().getHostName());
private void getStreams() throws IOException {
output = new ObjectOutputStream( client.getOutputStream());
output.flush();
input = new ObjectInputStream( client.getInputStream());
private void processConnection() throws IOException {
while( true ){
try {
message = ( String ) input.readObject();
System.out.print( message );
InputStreamReader isr = new InputStreamReader( System.in);
BufferedReader br = new BufferedReader( isr );
String line = null ;
line = br.readLine();
output.writeObject(line);
output.flush();
catch( ClassNotFoundException cnfe) {
System.out.println( "\nUnknown object type received");
} //end processConnection
private void closeConnection() {
System.out.println( "\nClosing connection");
try {
output.close();
input.close();
client.close();
catch( IOException ioe ) {
ioe.printStackTrace();
public static void main( String args[] ) {
Client c;
if( args.length == 0 )
c = new Client( "127.0.0.1" );
else
c = new Client( args[0] );
c.runClient();
maybe you should
1. Use code tags so the posted code is understandable
2. Stop marking your post urgent, that just puts peoples backs up
3. Stop posting the question every couple of hours on every imaginable forum.. Post it once and start a single dialog
Similar Messages
-
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 -
Executing exe files from java program
Hi,
I need to execute exe files and pass arguments to them from my java code.
Can you give me guidelines to do that as i haven't done that before.
Thanks.http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html
getRuntime() method.
exec() method. -
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 -
I need urgent help to remove unwanted adware from my iMac. Help!
I need urgent help to remove unwanted adware from my iMac. I have somehow got green underline in text ads, pop up ads that come in from the sides of the screen and ads that pop up selling similar products all over the page wherever I go. Its getting worse and I have researched and researched to no avail. I am really hestitant to download any software to remove whatever it is that is causing this problem. I have removed and reinstalled chrome. I have cleared Chrome and Safari cookies. Checked extensions, there are none to remove. I need to find an answer on how to get rid of these ads. Help please!
You installed the "DownLite" trojan, perhaps under a different name. Remove it as follows.
Back up all data.
Triple-click anywhere in the line below on this page to select it:
/Library/Application Support/VSearch
Right-click or control-click the line and select
Services ▹ Reveal in Finder (or just Reveal)
from the contextual menu.* A folder should open with an item named "VSearch" selected. Drag the selected item to the Trash. You may be prompted for your administrator login password.
Repeat with each of these lines:
/Library/LaunchAgents/com.vsearch.agent.plist
/Library/LaunchDaemons/com.vsearch.daemon.plist
/Library/LaunchDaemons/com.vsearch.helper.plist
/Library/LaunchDaemons/Jack.plist
/Library/PrivilegedHelperTools/Jack
/System/Library/Frameworks/VSearch.framework
Some of these items may be absent, in which case you'll get a message that the file can't be found. Skip that item and go on to the next one.
Restart and empty the Trash. Don't try to empty the Trash until you have restarted.
From the Safari menu bar, select
Safari ▹ Preferences... ▹ Extensions
Uninstall any extensions you don't know you need, including any that have the word "Spigot" in the description. If in doubt, uninstall all extensions. Do the equivalent for the Firefox and Chrome browsers, if you use either of those.
This trojan is distributed on illegal websites that traffic in pirated movies. If you, or anyone else who uses the computer, visit such sites and follow prompts to install software, you can expect much worse to happen in the future.
You may be wondering why you didn't get a warning from Gatekeeper about installing software from an unknown developer, as you should have. The reason is that the DownLite developer has a codesigning certificate issued by Apple, which causes Gatekeeper to give the installer a pass. Apple could revoke the certificate, but as of this writing, has not done so, even though it's aware of the problem. It must be said that this failure of oversight is inexcusable and has seriously compromised the value of Gatekeeper and the Developer ID program. You cannot rely on Gatekeeper alone to protect you from harmful software.
*If you don't see the contextual menu item, copy the selected text to the Clipboard by pressing the key combination command-C. In the Finder, select
Go ▹ Go to Folder...
from the menu bar and paste into the box that opens by pressing command-V. You won't see what you pasted because a line break is included. Press return. -
Need urgent help in listing out checklist from DBA prespective for BI Implementation Project
Hello Guys,
We are in Designing phase Data Modeling of PDW/APS Implementation Project.
I need urgent help in making a checklist from a DBA perspective.
Like what are things ill be needing at a time of implementation/Deployment.
Your expert comments and help will be highly appreciated.
Thank you,
Anish.S
AsandeenYou can get good summary of checklist from this article about
DBA checklist for data warehousing.
Which highlights on below pointers:
New system or old. (I.e. Up-gradation vs starting from scratch)
Complexity of SQL Server architecture
SQL Server storage
Determining SQL Server processing power
SQL Server installation consideration
SQL Server configuration parameter
SQL Server security
SQL Server Database property
SQL Server jobs and automation
Protecting SQL Server data
SQL Server health monitoring and check ups
SQL Server ownership and control
based on my real time experience, I will suggest you to keep an eye on
Load performance (It will be useful when your database(Warehouse) will have huge amount of data)
System availability (Check for Windows update and up time configuration)
Deployment methodology should be planned in advance. Development of packages and respective objects should be done systematically.
Source control mechanism
Disk space and memory usage
You might or might not have full rights on production environment, so be prepared to analyze production environment via Select statements [I guess you got my point]
Proper implementation of Landing , Staging and Mart tables.
Column size (this can drastically decrease your database size)
Usage of indexes (Index are good, but at what cost?)
I hope this will assist you in building your check list. -
I need to call a batch file from java and pass arguments to that Batch file
Hi,
I need to call a batch file from java and pass arguments to that Batch file.
For example say: The batch file(test.bat) contains this command: mkdir
I need to pass the name of the directory to the batch file as an argument from My Java program.
Runtime.getRuntime().exec("cmd /c start test.bat");
How to pass argument to the .bat file from Java now ?
regards,
Krish
Edited by: Krish4Java on Oct 17, 2007 2:47 PMHi Turing,
I am able to pass the argument directly but unable to pass as a String.
For example:
Runtime.getRuntime().exec("cmd /c start test.bat sample ");
When I pass it as a value sample, I am able to receive this value sample in the batch file. Do you know how to pass a String ?
String s1="sample";
Runtime.getRuntime().exec("cmd /c start test.bat s1 ");
s1 gets passed here instead of value sample to the batch file.
Pls let me know if you have a solution.
Thanks,
Krish -
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
-
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 to call a .bat file from java code?
How to call a .bat file from java code? and how can i pass parameters to that .bat file?
Thanks in advancethanks for ur reply
but still i am getting the same error.
I am trying to run a .bat file of together tool, my code looks like below
import java.lang.Runtime;
import java.lang.Process;
import java.io.File;
class SysCall{
public static void main(String args[]){
String cmd="D://Borland//Together6.2//bin//Together.bat -script:com.togethersoft.modules.qa.QA -metrics out:D://MySamples//Metrics// -fmt:html D://Borland//Together6.2//samples//java//CashSales//CashSales.tpr";
//String path="D://Borland//Together6.2//bin//Together.bat ";
Runtime r= Runtime.getRuntime(); //Declare the system call
try{
System.out.println("Before batch is called");
Process p=r.exec(cmd);
System.out.println(" Exit value =" + p.exitValue());
System.out.println("After batch is called");
/*can produce errors which must be caught*/
catch(Exception e) {
e.printStackTrace();
System.out.println (e.toString());
I am getting the below exception
Before batch is called
java.lang.IllegalThreadStateException: process has not exited
at java.lang.Win32Process.exitValue(Native Method)
at SysCall.main(SysCall.java:17)
java.lang.IllegalThreadStateException: process has not exited -
How can i call forpro prg file from java
Hai friends,
I have a doubt,clear it.
how can i call forpro prg file from java file
by,
N.VijayThanks to your reply,
I have some print statements in my foxpro program file.
Then i like to invoke that foxpro file from my java file
This want i want..,
by,
N.Vijay -
How to Call .XDO file From Java Program
Hi,
I have developed a report in using BI Publisher version 10.1.3.
I created the report and it only created XDO files. If I want to call XDO file from Java program how I can do that.
What are the APIs available to do that.
Thanks
-AshutoshHi,
the JavaAPI didn't work with the xdo-Files. But you can create a proxy stub for the Web Service API of BI Publisher which uses the xdo's in the repository.
regards
Rainer -
How to make Exe file of java program
how we can make the exe file of java programs. that can run independently of particuler JVM. like in VB we can create exe files.
- early thanksCheck this out:
http://search.java.sun.com/search/java/index.jsp?qp=%2B
orum%3A31&nh=10&qt=create+exe+file+program&col=javaforu
s -
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.. -
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...
Maybe you are looking for
-
In R12.1.3 we get FRM-40735: ON-ERROR trigger raised unhandled exception.
Hello, after we upgrade to R12.1.3 users are getting FRM-40735: ON-ERROR trigger raised unhandled exception ORA-01001, can you please help to resolve this issue immediately.
-
Color wheel pops up for minutes on end when trying to perform simple tasks?
Ever since last night, my Macbook Pro (purchased in September of 2009), has been incredibly slow. I noticed it first while trying to browse the web. Out of nowhere, while doing things, like browsing Facebook, that I could normally do easily and witho
-
I unfortunately will be using a PC in my new job. I would like to use my iTunes music library at work (that is currently on my Mac). I have bought an external drive to copy/back my music library. 1. What formatting do I use with my new external drive
-
How do I turn off my pop up blocker on my phone
How do I turn my pop up blocker on my Samsung note 3 cell phone. Duplicate of [/questions/1018021]
-
Why do the Tool Bars not show when opening new window?
Here is a short jing video that clearly shows the issue http://screencast.com/t/epteOKIy