Tomasz Mularczyk
Tomasz Mularczyk

Reputation: 36179

Ho to read .java file in executable jar

I'm using this code to load a .java file and do the search:

public class FindClassName {

    public static void main(String[] args) {
        Logger logger = Logger.getLogger("MOJ.Logger");

        try(Scanner scanner = new Scanner(new FileReader("./src/string/FindClassName.java"))){
            String pattern = "class\\s*(\\w+)\\s*";
            List<String> list = new LinkedList<>();
            while(scanner.hasNext()){
                String line = scanner.nextLine();
                Matcher matcher = Pattern.compile(pattern).matcher(line);
                while(matcher.find()){
                    list.add(matcher.group(1));
                }
            }
            System.out.println(list);
        }catch(IOException exception){
            logger.info("Couldn't read file");
        }
    }


    static class CHUJ{

    }

}

it works but when I export this to executable .jar file then It can't load file to reader. I've read that I need to use:

FindClassName.class.getResource("FindClassName.java");

but this gives me NullPointerException. I tried many different approaches but still couldn't load that .java file to reader with getResource() or getResourcesAsStream().

I know there are tons of questions like that, but I couldn't find the solution.

EDIT

this is strange, this code now runs in executable jar(with resources included) but not within eclipse... why? is there a better way?

public class FindClassName {

    public static void main(String[] args) throws FileNotFoundException {
        Logger logger = Logger.getLogger("MOJ.Logger");

        //String directory = "./src/string/FindClassName.java"; //to będzie działać w eclipsie, ale jak zrobisz z tego jar to wszystkie pliki .java zostaną skompilowane na .class
        InputStream directory = FindClassName.class.getResourceAsStream("FindClassName.java");

        try(Scanner scanner = new Scanner(new InputStreamReader(directory))){
            String pattern = "class\\s*(\\w+)\\s*";
            List<String> list = new LinkedList<>();
            while(scanner.hasNext()){
                String line = scanner.nextLine();
                Matcher matcher = Pattern.compile(pattern).matcher(line);
                while(matcher.find()){
                    list.add(matcher.group(1));
                }
            }
            System.out.println(list);
        }
    }


    static class CHUJ{

    }

}

Upvotes: 1

Views: 1161

Answers (2)

John
John

Reputation: 4006

Something like this should do it:

C:\>java -jar JarFileExample.jar
package com.mycompany.myproject;

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

public class JarFileExample {

        public static void main(String[] args) throws Exception {
                InputStream is = JarFileExample.class.getResourceAsStream("/com/mycompany/myproject/JarFileExample.java");
                writeFileToConsole(is);
        }

        private static void writeFileToConsole(InputStream is) throws IOException {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                for(String str = reader.readLine(); str != null; str = reader.readLine()) {
                        System.out.println(str);
                }
                reader.close();
        }

}

C:\>

The source code can easily be included in the jar file but depends on the tool you are using to create the jar. I used eclipse and basically the method described here: Eclipse: include source code while exporting as runnable jar

Upvotes: 1

Manuel Vieda
Manuel Vieda

Reputation: 296

Problem:

When you create an excecutable JAR (Actually any jar) all the .java files are compiled and transformed into .class files that can be used then by the JVM.

Solution:

One solution is to include the source code directory as additinal resource directory (Not a normal usecase):

<build>
    <resources>
        <resource>
            <directory>${project.build.sourceDirectory}</directory>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>

See this link for more information or this video using Eclipse

Upvotes: 2

Related Questions