Shank
Shank

Reputation: 33

Problem with calling a run file from c# Application

I am using a C# Application to call a Batch file which compiles and run a java program. (This is a scraper project which grabs content from websites.) The batch file consists of following command:

java -classpath core.jar;mysql.jar;realtouch.jar; com.parser.MainClass C:/wamp/www/C21_real2/properties http://www.realestate.com.au/realestate/agent/century+21+harbourside+neutral+bay/tzrjnd

This batch file is working fine,when i go to the folder and double click on the batch file. But when i am calling this run file through my application using System.Diagnostics.Process, it says:

Could not find the main class com.parser.MainClass. Program will exit now.

And command window will exit within seconds.

I am calling the program from C# as follows:

    Process batch = new Process();

    string pathtoRunFile="E:\\newFiles\\run.bat";

    batch.StartInfo.FileName = PathtoRunFile;
    batch.StartInfo.Arguments = "";

    batch.StartInfo.UseShellExecute = true;
    batch.Start();

    batch.WaitForExit();

Please someone help me ASAP. I am really confused why this is not working when i am calling it from my application. I am not much of a Java developer. So is this a problem with my main Java program? If so how to solve this? What I need is to run the batch file from my C# application.

The structure of the newfiles folder is as follows: (contains only files)

Upvotes: 2

Views: 1312

Answers (3)

Chris Fulstow
Chris Fulstow

Reputation: 41882

You might need to make your newFiles folder the current directory, so the java vm can find your files. Try adding this to your batch file:

E:
cd E:\newFiles\

Upvotes: 0

Murph
Murph

Reputation: 10190

Its probably a path issue - a difference between where in your directory tree the calling program has as its current directory and where the batch file is.

To test, open a command prompt, make sure you're not in e:\newFiles and run e:\newFiles\run.bat - I'd expect it to fail in the same way.

To fix you need to either a) add the path to "com.parser.MainClass" or b) set the current directory within the C# application.

Upvotes: 2

codeape
codeape

Reputation: 100766

Set the working directory:

batch.StartInfo.WorkingDirectory = "E:\\newFiles";

Upvotes: 9

Related Questions