Pregunta

I am facing an issue here. I need to create a jar file of a spring batch project. I exported the project as a runnable jar from eclipse but it not able to find out the config xml file in the jar when i try to rung the jar using command prompt. The work around I came up with is to copy the config file in the same folder where i have the jar but it is definitely something undesirable. It works when i run the app in eclupse as java application but not wen i convert the app into a jar and run it from command prompt.

This is my main class:

public class RunApp {
public static void main(String[] args) {

    String[] springConfig = { "hello-job.xml" };

    ApplicationContext context = new ClassPathXmlApplicationContext(
            springConfig);

    JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
    Job job = (Job) context.getBean("helloJob");

    try {

        JobExecution execution = jobLauncher.run(job, new JobParameters());
        System.out.println("Exit Status : " + execution.getStatus());

    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        ((AbstractApplicationContext)context).close();
    }

    System.out.println("Done");

}
}

I need a solution where I can pass the "hello-job.xml" file in the ClassPathXmlApplicationContext as a stream or any way I can access my xml file from the jar formed.

My hello-job.xml is present under src/main/resources/hello-job.xml

¿Fue útil?

Solución

You have 3 possibilities. In all three cases you can keep the hello-job.xml file in src/main/resources folder. I'll start with the easier variant:

1. With the condition that you're using Maven for dependency management, just add a configuration for the maven-assembly-plugin in which you specify the main class + to include you dependencies in the final executable jar. When you run mvn clean package it will build an executable jar on which you can run java -jar myjar-jar-with-dependencies.jar, no matter where you put that jar.

The following is the configuration for the plugin which you need to add to you pom.xml file:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.mypackage.RunApp</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2 You have to create a MANIFEST.MF file in your src/main/resources/META-INF directory, in which you specify the main class by adding the line Main-Class: com.mypackage.RunApp (note that you have to insert a newline after each line you write).

After you build your package, you have to put that executable jar in a directory where ALL your dependencies are as well.

3 This is similar to the second variant (you also have to put your packaged jar in the same directory as your dependencies), the difference being that you tell Maven to build your MANIFEST.MF file. This is also done by configuring a plugin:

<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.mypackage.RunApp</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Otros consejos

Looks like the XML is not present in your class path when you run the stand alone code.Try setting the class path in the command prompt and then run the class. Link on how to set the class path http://docs.oracle.com/javase/tutorial/essential/environment/paths.html HTH

I think there is no need for you to externalize the xml file unless you want to. But since its under the /resources path and i guess you use Maven then it will automatically put that jar into the ClassPath. No need for you to do that explicitaly.

Now suppose you want to use it in your other xml files for fetching the beans from it you have to just import it in your other xml

<import resource="classpath:/config/hello-job.xml" />

and when you run your jar it will automatically be picked up by your code

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top