Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Illegal Start of Expression // Creating, Compiling, and Executing Runtime Project

  1. #1
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Post Illegal Start of Expression // Creating, Compiling, and Executing Runtime Project

    What's up forum,

    I've been extending my little project for some time now. So far what I'm trying to have this program create an empty .java file, then proceeding to compile and execute it. I'm aware it is still not complete. I haven't added the overwrite empty .java part of the code.

    import java.io.*;
     
    public class Mercury {
     
        public static void main(String[] args) {
     
    	    Vector <String> ubicac = new Vector <String>(0, 1);
    	    Vector <String> nombre = new Vector <String>(0, 1);
    	    Vector <String> direct = new Vector <String>(0, 1);
     
    	// SIMULADOR LOGIC
     
    	    ubicac.addElement("C:\\Users\\Andrew\\Desktop//");
    	    nombre.addElement("Test.java");
    	    direct.addElement(ubicac.elementAt(ubicac.size()-1)+nombre.elementAt(nombre.size()-1));
     
    	// CREATOR
     
    		createFile(direct.elementAt(direct.size()-1));
     
    		public static void createFile(String S){
                File file = new File(S);
                boolean blnCreated = false;
    		    try {  blnCreated = file.createNewFile(); }
                catch(IOException ioe) { System.out.println("Error while creating a new empty file :" + ioe); }
                System.out.println("Was file " + file.getPath() + " created ? : " + blnCreated);
            }
     
        // COMPILER & EXECUTER - [!] REVISION NECESARIA: Recibir Param String que coincida con Vector PLACE.elementAt(i) (decidido por LOGIC)
     
            try { runProcess("javac " + nombre.elementAt(nombre.size()-1));
                  runProcess("java Test");
            } catch (Exception e) { e.printStackTrace(); }
     
            public static void printLines(String name, InputStream ins) throws Exception {
                String line = null;
                BufferedReader in = new BufferedReader( new InputStreamReader(ins) );
                while ((line = in.readLine()) != null) { 
                    System.out.println(name + " " + line);
                }
            }
     
            private static void runProcess(String command) throws Exception { //
            Process pro = Runtime.getRuntime().exec(command);
            printLines(command + " stdout:", pro.getInputStream());
            printLines(command + " stderr:", pro.getErrorStream());
            pro.waitFor();
            System.out.println(command + " exitValue() " + pro.exitValue());
            }
     
        }
     
    }

    So far I'm trying to compile my code, and unfortunately its giving a basic problem apparently.
    Shame on me really. I can't figure this one out.
    I am creative just that I'm more of the empirical type learner.

    C:\Users\Andrew\Desktop>javac Mercury.java
    Mercury.java:21: error: illegal start of expression
    public static void createFile(String S){
    I'm using Notepad++ w/cmd prompt.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Illegal Start of Expression // Creating, Compiling, and Executing Runtime Project

    You need to fix your formatting. Where does each method start and end? You can't have methods directly inside other methods, and you can't have expressions outside of methods. Your (lack of) formatting obscures these things, which is what's giving you trouble. Shoot for something like this:

    class Example{
     
       int variable;
       String string;
     
       public void method(){
          codeInMethod();
          moreCode();
       }
     
       public void otherMethod(){
          //indentations matter
          try{
             code();
          }
          catch(Exception e){
             //don't ignore exception
             e.printStackTrace();
          }
       }
     
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Illegal Start of Expression // Creating, Compiling, and Executing Runtime Project

    There we go. Thank you. I keep forgetting constructor is also a method.

    --- Update ---

    import java.io.*;
     
    public class Mercury {
     
        public static void createFile(String S){
            File file = new File(S);
            boolean blnCreated = false;
            try {  blnCreated = file.createNewFile(); }
            catch(IOException ioe) { System.out.println("Error while creating a new empty file :" + ioe); }
            System.out.println("Was file " + file.getPath() + " created ? : " + blnCreated);
        }
     
        public static void printLines(String name, InputStream ins) throws Exception {
            String line = null;
            BufferedReader in = new BufferedReader( new InputStreamReader(ins) );
            while ((line = in.readLine()) != null) { 
                System.out.println(name + " " + line);
            }
        }
     
        private static void runProcess(String command) throws Exception { //
            Process pro = Runtime.getRuntime().exec(command);
            printLines(command + " stdout:", pro.getInputStream());
            printLines(command + " stderr:", pro.getErrorStream());
            pro.waitFor();
            System.out.println(command + " exitValue() " + pro.exitValue());
        }
     
        // Some variables declared to test my code:
     
    	Vector <String> ubicac = new Vector <String>(0, 1);
        Vector <String> nombre = new Vector <String>(0, 1);
        Vector <String> direct = new Vector <String>(0, 1);
     
        ubicac.addElement("C:\\Users\\Andrew\\Desktop//");
        nombre.addElement("Test.java");
        direct.addElement(ubicac.elementAt(ubicac.size()-1)+nombre.elementAt(nombre.size()-1));
     
        // Applying my Methods:
        public static void main(String[] args) {    
     
    	createFile(direct.elementAt(direct.size()-1));
     
            try { runProcess("javac " + nombre.elementAt(nombre.size()-1));
                  runProcess("java Test");
            } catch (Exception e) { e.printStackTrace(); }
     
        }
     
    }

    At least I'm getting a different type of error now. By the way, how can I get the the data I place in the " # " color coded like your post?

    Mercury.java:35: error: <identifier> expected
    ubicac.addElement("C:\\Users\\Andrew\\Desktop//");
    ^
    Mercury.java:35: error: illegal start of type
    ubicac.addElement("C:\\Users\\Andrew\\Desktop//");
    ^
    Mercury.java:36: error: <identifier> expected
    nombre.addElement("Test.java");
    ^
    Mercury.java:36: error: illegal start of type
    nombre.addElement("Test.java");

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Illegal Start of Expression // Creating, Compiling, and Executing Runtime Project

    Use the highlight=java /highlight tags instead of the code tags for syntax highlighting.

    Like I said, you can't have expressions outside of methods. You can have declarative statements like Vector nombre = new Vector(); at the class level, but you can't do things like nombre.addElement() outside of a method.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Illegal Start of Expression // Creating, Compiling, and Executing Runtime Project

    God. I am a noob. Thank you again. I get it now.

    --- Update ---

    God. I am a noob. Thank you again. I get it now.

    import java.io.*;
    import java.util.*;
     
    public class Mercury {
     
        public static void createFile(String S){
            File file = new File(S);
            boolean blnCreated = false;
            try {  blnCreated = file.createNewFile(); }
            catch(IOException ioe) { System.out.println("Error while creating a new empty file :" + ioe); }
            System.out.println("Was file " + file.getPath() + " created ? : " + blnCreated);
        }
     
        public static void printLines(String name, InputStream ins) throws Exception {
            String line = null;
            BufferedReader in = new BufferedReader( new InputStreamReader(ins) );
            while ((line = in.readLine()) != null) { 
                System.out.println(name + " " + line);
            }
        }
     
        private static void runProcess(String command) throws Exception { //
            Process pro = Runtime.getRuntime().exec(command);
            printLines(command + " stdout:", pro.getInputStream());
            printLines(command + " stderr:", pro.getErrorStream());
            pro.waitFor();
            System.out.println(command + " exitValue() " + pro.exitValue());
        }
     
        public static void main(String[] args) {    
     
    	    Vector <String> ubicac = new Vector <String>(0, 1);
            Vector <String> nombre = new Vector <String>(0, 1);
            Vector <String> direct = new Vector <String>(0, 1);
     
            ubicac.addElement("C:\\Users\\Andrew\\Desktop//");
            nombre.addElement("Test.java");
            direct.addElement(ubicac.elementAt(ubicac.size()-1)+nombre.elementAt(nombre.size()-1));
     
    		createFile(direct.elementAt(direct.size()-1));
     
            try { runProcess("javac " + nombre.elementAt(nombre.size()-1));
                  runProcess("java Test");
            } catch (Exception e) { e.printStackTrace(); }
     
        }
     
    }



    So the program is compiling, and executing. Although it could not compile "Test.java" since there is no java code in it. Next step is to investigate how to overwrite a .java file.

    --- Update ---

    I've modified part of the code to at least be able to write something in it.

        .
        .
        .
        public static void createFile(String S){
            File file = new File(S);
            boolean blnCreated = false;
            try {  blnCreated = file.createNewFile(); }
            catch(IOException ioe) { System.out.println("Error while creating a new empty file :" + ioe); }
            System.out.println("Was file " + file.getPath() + " created ? : " + blnCreated);
     
    	FileWriter writer = new FileWriter(file);
            writer.write("This\n is\n an\n example\n"); 
            writer.flush();
            writer.close();
        }
        .
        .
        .

    I had to place the FileWriter whithin the same method otherwise Java was not able to recognize object file "file" in a different method. I'm okay with that since it would be best to create and write the String(java code) in one go before compiling. But I'd like to be able to modify or overwrite a .java file at any given time. How can I reference an object from a different method to another. Is there a way of doing this?

    Most importantly I'm getting this error prompt from the cmd when I try to compile it:

    C:\Users\Andrew\Desktop>javac Mercury.java
    Mercury.java:13: error: unreported exception IOException; must be caught or decl
    ared to be thrown
    FileWriter writer = new FileWriter(file);
    ^
    Like I said, I am empirical type Just trying to do stuff I just haven't seen yet. So I'd appreciate if you could help me or reference me to somewhere so I can learn about the IOException.

    I'll be taking a look at this tommorow :I I still have a job to report to.

    Thanks for all the help you've given me. I feel I'm finally making progress.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Illegal Start of Expression // Creating, Compiling, and Executing Runtime Project

    That error explains exactly the problem: creating a new FileWriter can throw an IOException, so you have to either put it inside a try block or pass the exception up the chain. Recommended reading: Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Executing cmd from Java, Compiling and Executing IN Runtime
    By Andrew R in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 9th, 2013, 10:00 AM
  2. Illegal start of expression
    By Tedstriker in forum What's Wrong With My Code?
    Replies: 22
    Last Post: April 14th, 2013, 06:31 AM
  3. Illegal start of expression
    By bad_newbie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 16th, 2013, 07:57 PM
  4. Illegal start of expression!
    By NoobOfTheMonth in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 11th, 2013, 08:53 PM
  5. Help With illegal start of expression
    By inshal in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 9th, 2013, 12:20 PM

Tags for this Thread