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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: JavaCompiler is not finding the file

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post JavaCompiler is not finding the file

    Hello.

    I am trying to write a little script, that shall compile another script. For that, I am using the "JavaCompiler": import javax.tools.JavaCompiler;
    However the file, I want to compile, cannot be found, when I run the script; although it in the same folder, as the other script.
    That's the error I am receiving:

    javac: file not found: myAccountName/TheFolderItIsIn/test.java
    Usage: javac <options> <source files>
    use -help for a list of possible options

    There's my code:

    import javax.tools.JavaCompiler;
    import javax.tools.ToolProvider.*;
    import java.lang.Object;
    import javax.tools.ToolProvider;
    import javax.tools.StandardJavaFileManager;
     
    public class javaCompiler {  
     
    public static void main(String[] args) {  
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            System.out.println(compiler);
            int result = compiler.run(null, null, null, "test.java");
        }  
    }
    Why is this happening? Do I need to enter a path into the int result.... line?
    Thanks for your help.
    Last edited by Compiler; June 4th, 2020 at 10:34 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaCompiler is not finding the file

    Your code works for me with java 1.7. It doesn't find the JavaCompiler with java 1.8.

    javac: file not found: myAccountName/TheFolderItIsIn/test.java
    Where does the part in Red come from? It is not in the code.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JavaCompiler is not finding the file

    Quote Originally Posted by Norm View Post
    Your code works for me with java 1.7. It doesn't find the JavaCompiler with java 1.8.


    Where does the part in Red come from? It is not in the code.
    .
    That is the path, the file is found in; I changed my name and the folders name for this post.
    I compiled it again, and it seems, to be my fault; why did I insert the (in your post) red part, into this thread, when it is not in the compiler? Sorry for that: :-(

    So here's the error message again:

    javac: file not found: test.java
    Usage: javac <options> <source files>
    use -help for a list of possible options

    So you mean, I should use Java 1.7?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaCompiler is not finding the file

    It is better when the posted code and the posted error messages are in synch.

    should use Java 1.7?
    No. Your code is not getting the error I was: compiler was null.
    I have not tracked down why my the compiler variable is null with the default java command on my system. The code works with all the other java commands I've tested with.

    What happens if you manually use the javac command to compile test.java?

    My problem was the java command I was using was from a JRE. The other java commamds were from JDKs
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JavaCompiler is not finding the file

    Quote Originally Posted by Norm View Post
    It is better when the posted code and the posted error messages are in synch.
    Eh...sorry, don't quite understand what you mean with posting them in synch.

    Quote Originally Posted by Norm View Post
    No. Your code is not getting the error I was: compiler was null.
    I have not tracked down why my the compiler variable is null with the default java command on my system. The code works with all the other java commands I've tested with.
    Shall I use another command to run the script?
    Quote Originally Posted by Norm View Post
    What happens if you manually use the javac command to compile test.java?
    If I run it manually, through the command line, nothing happens, but if I run it using the Java "Runner" from VS Code, in my Editor, it is opening up a GUI Window (and that's what it's supposed to do).

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaCompiler is not finding the file

    javac: file not found: test.java
    That says the javac program did not find the test.java file. Are you sure the file is in the current directory when your code is executed?
    Create a File object for the test.java file and print its absolute path to see where the program thinks it is located.


    By "in synch" I meant that the error messages would be for the posted code, not for a different version of the code.
    javac: file not found: myAccountName/TheFolderItIsIn/test.java
    and
    int result = compiler.run(null, null, null, "test.java");
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JavaCompiler is not finding the file

    First of all, I created a new folder, and I put both the scripts, the actual compiler script and the test file in there.

    I then added this, to the test file, and got that error:

    error: cannot find symbol
        static File file = File("C:\\Hilfsassistent\\main.java");
                           ^
      symbol:   method File(String)
      location: class main
    1 error

    import java.io.File;
     
    public class main{
        public static void main(String[] args) {
            pathname();
        } 
     
    public static String pathname() {
            static File file = File("C:\\Hilfsassistent\\test.java");
            System.out.println("Path : " + file.getAbsolutePath());
        }

    But when I compile it, it tells me, that it is not finding the file (As mentioned above).

    Next up I tried adding one line of code.
    But it doesn't seem to be useful, because it prints the following:
    Working Directory = /

    That's what I'm talking about:

    System.out.println("Working Directory = " + System.getProperty("user.dir"));

    Thanks for your help.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaCompiler is not finding the file

    You need to use a new statement to create an instance of the File class.
    File aFile = new File("test.java");
    Also the code should use the same path in the File class constructor as is used with the compiler.run method call. The objective is to see where the program is looking to find the file.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JavaCompiler is not finding the file

    I get the following error here:

    error: cannot find symbol
            File aFile = new File("test.java");
            ^
      symbol:   class File
      location: class javaCompilera
    /Users/myusername/T/javaCompilera.java:14: error: cannot find symbol
            File aFile = new File("test.java");
                             ^
      symbol:   class File
      location: class javaCompilera
    2 errors

    What do I need to import, that it knows this object type?
    Can't I just somewhere in the following line, give the program a path?

    int result = compiler.run(null, null, null, "test.java");

    public static void main(String[] args) {  
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            System.out.println(compiler);
            //int result = compiler.run(null, null, null, "test.java");   
            File aFile = new File("test.java");

    Thanks for your help.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaCompiler is not finding the file

    What do I need to import, that it knows this object type?
    Look at the API documentation for the java SE classes: https://docs.oracle.com/javase/8/docs/api/index.html
    Find the class in the lower left frame, click on it and the API doc for the class will be shown in the main frame.
    The package the class is in is shown near the top left.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JavaCompiler is not finding the file

    Quote Originally Posted by Norm View Post
    Look at the API documentation for the java SE classes: https://docs.oracle.com/javase/8/docs/api/index.html
    Find the class in the lower left frame, click on it and the API doc for the class will be shown in the main frame.
    The package the class is in is shown near the top left.
    Sorry :-( ; there are so many classes; could you just tell me the name of the class I am looking for? Otherwise it would be kind of hard to find... Thanks :-)

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaCompiler is not finding the file

    What do I need to import, that it knows this object type?
    That was asked about this error message:
    error: cannot find symbol
    File aFile = new File("test.java");
    ^
    symbol: class File
    So I assumed you were looking for the documentation for the File class.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JavaCompiler is not finding the file

    Yes, thanks, I don't get that error anymore, but how can I now locate the path of the file, where the program looks for it, using the
    File aFile = new File("test.java");

    part?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaCompiler is not finding the file

    how can I now locate the path of the file
    Didn't you already do that in post#7?

    Besides printing out the absolute path, you could print out the value returned by the exists method.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JavaCompiler is not finding the file

    I did it like this now, and it says, that the Working Directory is null.
    Working Directory = null
    Path : /test.java

    That's the code:

    public static void main(String[] args) {  
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            //int result = compiler.run(null, null, null, "test.java");   
            File aFile = new File("test.java");
            System.out.println("Working Directory = " + System.getProperty("test.java"));
     
            //File file = new File("C:\\userName\\T\\test.java");
            System.out.println("Path : " + aFile.getAbsolutePath());
    What can I do, because it says it is null. ?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaCompiler is not finding the file

    it says it is null. ?
    Where is there a property named: "test.java" ??? Look at the code in post#7 for a valid property.
             System.out.println("Working Directory = " + System.getProperty("test.java"));


    Path : /test.java
    The file's absolute path is different for your system. Why isn't there any path before the /?
    Here is what I get:
    Path : D:\JavaDevelopment\Testing\ForumQuestions13\test.j ava
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JavaCompiler is not finding the file

    it seems like it's printing out at the path; whatever I put into the "" in the new File(""); part:

    Output:
    Path : /C:\T\test.java

    My Code;
    public static void main(String[] args) {  
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            System.out.println(compiler);
            //int result = compiler.run(null, null, null, "test.java");   
            File aFile = new File("test.java");
            System.out.println("Working Directory = " + System.getProperty("user.dir"));
            //System.out.println("Working Directory = " + System.getProperty("test.java"));
            //System.out.println(aFile);
            System.out.println("Path : " + aFile.getAbsolutePath());
            File file = new File("C:\\T\\test.java");
            System.out.println("Path : " + file.getAbsolutePath());
        }

    I'm using a Mac. Do you thing that influences, what I get as a result?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaCompiler is not finding the file

    Why is only one line of output printed? I see 4 print statements.
           File aFile = new File("test.java");
            System.out.println("Working Directory = " + System.getProperty("user.dir"));  // what prints????
            System.out.println("Path : " + aFile.getAbsolutePath());  //>>>>>>>>>>>> WHAT does this print?

    I'm using a Mac.
    Then c:\ is not a correct path on a Mac
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JavaCompiler is not finding the file

    I now copied the right path of the file and pasted it in there. What I get then is this:

    com.sun.tools.javac.api.JavacTool@1b6d3586
    Working Directory = /
    Path : /Users/myname/T/test.java

    The code:

    public static void main(String[] args) {  
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            System.out.println(compiler);
            File file = new File("/Users/myname/T/test.java");
            //int result = compiler.run(null, null, null, "test.java");   
            int result = compiler.run(null, null, null, "/Users/myname/T/test.java");
            System.out.println("Working Directory = " + System.getProperty("user.dir"));
            System.out.println("Path : " + file.getAbsolutePath());

    How can I use my compiler component to print out the errors, it gets form the test.java file (if it has compile errors)?
    And why am I getting this error, when I try to replace the
     int result = compiler.run(null, null, null, "/Users/myname/T/test.java");
    with this:
     int result = compiler.run(null, null, null, file);

    The error I am getting when I try that:

    error: method run in interface Tool cannot be applied to given types;
            int result = compiler.run(null, null, null, file);
                                 ^
      required: InputStream,OutputStream,OutputStream,String[]
      found: <null>,<null>,<null>,File
      reason: varargs mismatch; File cannot be converted to String

    Can I somehow write the file variable into the compiler.run function, without having to type in the whole path?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaCompiler is not finding the file

    why am I getting this error,
    required: InputStream,OutputStream,OutputStream,String[]
    found: <null>,<null>,<null>,File
    reason: varargs mismatch; File cannot be converted to String
    The method requires a String object, not a File object.

    What happens when you specify the full path like here:
    int result = compiler.run(null, null, null, "/Users/myname/T/test.java");
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JavaCompiler is not finding the file

    That works fine, but I want the user be able to input a path into a GUI input field, and than that path should get compiled. Do you have any idea how I can achieve that, because I cannot input a String into my compiler.run() function. Also it would be nice to show the errors, from the compiled code (if there are any) in a text label, instead of them getting printed out in the console.
    But I don't know how I can do that. Do you have any suggestions there? Would be nice :-)
    Thanks

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaCompiler is not finding the file

    I cannot input a String into my compiler.run() function.
    Why not? The run method takes a String. That String could be obtained from the user.
    If you don't understand my answer, don't ignore it, ask a question.

  23. The Following User Says Thank You to Norm For This Useful Post:

    Compiler (June 18th, 2020)

  24. #23
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JavaCompiler is not finding the file

    I managed to do that now. Do you have any idea, how I can look in my script, how many (if any) errors I get from compiling the other script, and to than do sth, depending on how many errors the user has in the other script, that got compiled.
    I am thinking of an if-function here, but how can I count the errors in the script?

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaCompiler is not finding the file

    Are you asking how to look at what is written to the console by the javac command?
    Look at the System setErr and setOut methods. They may allow you to capture what is written to the console.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #25
    Junior Member
    Join Date
    Jun 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JavaCompiler is not finding the file

    I googled it and found out, that the setErr method allows me to write the errors from the console into e.g. a text file.
    What I was thinking of, is that it counts how many errors you got, e.g. somehow like that (using a command, but I don't know which one):

    //the variable errorNumbers is just a placeholder for what a command from an import would do
    int errorNumbers = 0;
     
    //now here I need a method that checks, how many errors you got, from compiling a script with the java command
     
    if(errorNumbers == 0) {
    System.out.println("You got 0 errors"!);
    } else if (errorNumbers <= 3) {
    System.out.println("You got three errors or less!");
    } else { System.out.println("You got more than three errors!"); }

    I don't want to use the things, that the javac command is printing out in the console, I want it to check how many errors I got, from compiling that script.

Page 1 of 2 12 LastLast

Similar Threads

  1. Finding full path of file
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: November 1st, 2013, 04:42 PM
  2. Scanner not finding text file
    By jhy2a in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 13th, 2012, 05:45 AM
  3. finding the amt of words and chars in a give file
    By mysticCHEEZE in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 3rd, 2011, 11:31 PM
  4. finding String in a file
    By nasi in forum Java Theory & Questions
    Replies: 12
    Last Post: May 31st, 2010, 01:16 PM
  5. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM

Tags for this Thread