Results 1 to 5 of 5

Thread: File contains no errors but won't run

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default File contains no errors but won't run

    When i try to run this in eclipse:

    Code:
    import java.io.File;
    import java.io.FileWriter;
    
    public class CDUtils {
      private CDUtils() {  }
    
      public static void open(String drive) {
        try {
            File file = File.createTempFile("realhowto",".vbs");
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);
            String vbs = "Set wmp = CreateObject(\"WMPlayer.OCX\") \n"
                       + "Set cd = wmp.cdromCollection.getByDriveSpecifier(\""
                       + drive + "\") \n"
                       + "cd.Eject";
            fw.write(vbs);
            fw.close();
            Runtime.getRuntime().exec("wscript " + file.getPath()).waitFor();
            // thanks to TrueJavaProgammer for the waitFor() tip!      
            // Runtime.getRuntime().exec("wscript " + file.getPath()).waitFor();
            // Thread.sleep(2000);
        }
        catch(Exception e){
            e.printStackTrace();
        }
      }
    
      public static void close(String drive) {
        try {
            File file = File.createTempFile("realhowto",".vbs");
            file.deleteOnExit();
            FileWriter fw = new FileWriter(file);
            // to close a CD, we need eject two times!
            String vbs = "Set wmp = CreateObject(\"WMPlayer.OCX\") \n"
                       + "Set cd = wmp.cdromCollection.getByDriveSpecifier(\""
                       + drive + "\") \n"
                       + "cd.Eject \n "
                       + "cd.Eject ";
            fw.write(vbs);
            fw.close();
            Runtime.getRuntime().exec("wscript " + file.getPath()).waitFor();
            // thanks to TrueJavaProgammer for the waitFor() tip!      
            // Runtime.getRuntime().exec("wscript "+ file.getPath());
            // Thread.sleep(2000);
        }
        catch(Exception e){
            e.printStackTrace();
        }
      }
    }
    It asks me if I want to run it has a java application or java applet, I get a error either way: 'selection does not contain a main type'

  2. #2
    Join Date
    Apr 2008
    Location
    Marquette, MI
    Posts
    15,252
    Mentioned
    138 Post(s)
    Quoted
    680 Post(s)

    Default

    You need a main method if you want to run it and not just import it as a class.
    Java Code:
    import java.io.File;
    import java.io.FileWriter;

    public class CDUtils {
      private CDUtils() {  }

      public static void main(String[] args) { }

      public static void open(String drive) {
        try {
            File file = File.createTempFile("realhowto",".vbs");
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);
            String vbs = "Set wmp = CreateObject(\"WMPlayer.OCX\") \n"
                       + "Set cd = wmp.cdromCollection.getByDriveSpecifier(\""
                       + drive + "\") \n"
                       + "cd.Eject";
            fw.write(vbs);
            fw.close();
            Runtime.getRuntime().exec("wscript " + file.getPath()).waitFor();
            // thanks to TrueJavaProgammer for the waitFor() tip!
            // Runtime.getRuntime().exec("wscript " + file.getPath()).waitFor();
            // Thread.sleep(2000);
        }
        catch(Exception e){
            e.printStackTrace();
        }
      }

      public static void close(String drive) {
        try {
            File file = File.createTempFile("realhowto",".vbs");
            file.deleteOnExit();
            FileWriter fw = new FileWriter(file);
            // to close a CD, we need eject two times!
            String vbs = "Set wmp = CreateObject(\"WMPlayer.OCX\") \n"
                       + "Set cd = wmp.cdromCollection.getByDriveSpecifier(\""
                       + drive + "\") \n"
                       + "cd.Eject \n "
                       + "cd.Eject ";
            fw.write(vbs);
            fw.close();
            Runtime.getRuntime().exec("wscript " + file.getPath()).waitFor();
            // thanks to TrueJavaProgammer for the waitFor() tip!
            // Runtime.getRuntime().exec("wscript "+ file.getPath());
            // Thread.sleep(2000);
        }
        catch(Exception e){
            e.printStackTrace();
        }
      }
    }

  3. #3
    Join Date
    Jan 2011
    Location
    Denver, CO
    Posts
    1,351
    Mentioned
    2 Post(s)
    Quoted
    72 Post(s)

    Default

    As Coh3n said, to run this program you need a main method containing the code you want to run. It's just like the begin...end. in Pascal.

    Also wanted to point out that you don't need to define a constructor if you're not gonna use it

  4. #4
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Ok thanks, gonna make a fake-ass GUI of a mario game hacker and troll my freind

  5. #5
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Ok now it's running but it does not open the Cd drive

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •