0

I am a newbie in Java programming. I have this class which should run a .bat file located on a folder in my Local Disk:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;



public class Execute {

    public static void main(String args[]) 
    {

        String stream = null;
        try {

            Process p = Runtime.getRuntime().exec(
                    "C:\\WINDOWS\\system32\\cmd.exe /c start C:\\Identify\\dll\\StartSample.bat");

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:");
            while ((stream = stdInput.readLine()) != null) 
            {
                System.out.println(stream);
            }

            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):");
            while ((stream = stdError.readLine()) != null) 
            {
                System.out.println(stream);
            }

            System.out.println("ended!!");
            System.exit(0);
        }
        catch (IOException e) 
        {
            System.out.println("exception happened: ");
            System.err.println(e.getMessage());
            System.exit(-1);
        }
    }

}

Whenever I run the .bat file, it works just as it should. However, when I run the class that I made, the command prompt displays "C:\palmuswebservice>java Error: Could not find or load main class"

I don't know what seems to be wrong. Can someone help me on how I can fix this?

greg-449
  • 109,219
  • 232
  • 102
  • 145
Kylie Irwin
  • 195
  • 3
  • 11
  • Do you try to run the java class from some IDE or command line? If command line, what are the commands you execute? – hammerfest Oct 06 '16 at 08:21
  • @hammerfest I run it from IDE. I run it as a Java Application in eclipse. – Kylie Irwin Oct 06 '16 at 08:24
  • What do you exactly do to run it? E.g. right click on the class text and select run/execute/whatever from the pop-up menu? (the class itself seems to be OK so it is probably some environment issue only) – hammerfest Oct 06 '16 at 08:29
  • @hammerfest that is what I do. I right click on the class>run as>Java Application, the cmd then runs and displays the error. – Kylie Irwin Oct 06 '16 at 08:32
  • 1
    I tried out your code nothing goes wrong it works fine. You can also use alternate approach as described [here](http://stackoverflow.com/questions/19103570/run-batch-file-from-java-code). Hope this will work. – legend Oct 06 '16 at 08:32
  • Does you application run from command line using java command ? – saurav Oct 06 '16 at 08:34
  • 1
    @KylieIrwin: possibly some environment issue in your Eclipse only then. What if you try to rebuild the project and execute again? – hammerfest Oct 06 '16 at 08:35
  • @saurav it is running using java command as well. – Kylie Irwin Oct 06 '16 at 08:59
  • @hammerfest i tried switching workspace and rebuilding the project as well, but still, it returns that error. – Kylie Irwin Oct 06 '16 at 08:59
  • @code_legend I was just wondering how you tried my code? Did you create a class on an existing project and just run it? On what IDE did you run it? – Kylie Irwin Oct 06 '16 at 09:07
  • @KylieIrwin Yes, I tried to run your code on some existing project using a sample batch file and it works. Have you check your path variable and preference section under windows in eclipse for installed jre ? – legend Oct 06 '16 at 09:41
  • @code_legend Yes I have, what about that? Installed JRE is jre1.8.0_101. – Kylie Irwin Oct 06 '16 at 09:47
  • @KylieIrwin Just for check have you followed all the correct procedure for executing the java program and naming convention as explained [here](http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean). – legend Oct 06 '16 at 10:13
  • @code_legend I will look into that and update you. Thank you! – Kylie Irwin Oct 06 '16 at 10:15
  • @KylieIrwin Hope u will find solution. – legend Oct 06 '16 at 10:17
  • @code_legend Hello there! I have tried running it following the procedure from the link that you gave me. Unfortunately, it still isn't working. Looking more into it, the error points to the main class of the .bat file. Although the .bat file works fine if I just run it as a .bat file. – Kylie Irwin Oct 06 '16 at 10:24
  • @KylieIrwin will you show me the .bat file, please. – legend Oct 06 '16 at 10:28
  • @code_legend this is what the .bat file contains '@echo off set CLASSPATH=%CLASSPATH%;f3bc4jav.jar set CLASSPATH=%CLASSPATH%;PalmSecureSample_Java.jar' '@echo on java com.fujitsu.frontech.palmsecure_smpl.PsSampleApl' – Kylie Irwin Oct 06 '16 at 10:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125095/discussion-between-code-legend-and-kylie-irwin). – legend Oct 06 '16 at 10:52

1 Answers1

0

You have to add a Manifest file in the JAR file. The Manifest file contains a key called Main-Class which should be the full name of the main class

The manifest file is located (within the JAR file) in META-INF\MANIFEST.MF

An example manifest file can look like this:
Manifest-Version: 1.0 Main-Class: full.class_.name.MainClass

(In Eclipse there is an option to export runnable JAR file which can easily make this file)

ramidzkh
  • 11
  • 4