Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Problem loading xml file

  1. #1
    Join Date
    Feb 2012
    Posts
    7

    Default Problem loading xml file

    I use this:

    Code:
    ApplicationContext context = new ClassPathXmlApplicationContext("myfile.xml");
    to load my ApplicationContext. myfile.xml is in src/main/resources
    My problem is that inside STS, maven puts the file in target/classes and everything is OK, but, when I create the JAR, myfile.xml is in /resources and it doesn't find the file.
    If I change the sentece by
    Code:
    ApplicationContext context = new ClassPathXmlApplicationContext("resources/myfile.xml");
    it works in the Jar but it doesn't work inside STS, because myfile.xml is at target/classes.

    I've burned Google and I can't find any solutions.
    I'm sure this is a common problem with a easy solution, that I can't obviously find. Thanks for your help and I apologize for my horrible English.
    Last edited by Wessiri; Feb 22nd, 2012 at 05:30 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Your file in the jar should be in the root of your classpath if it isn't you have done something strange in your build/packaging of the jar.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Feb 2012
    Posts
    7

    Default

    Well, I know now where is the problem. I've visited again this http://maven.apache.org/guides/getti...rces_to_my_JAR and it says "The simple rule employed by Maven is this: any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR.". I have another file (an SQL script file) in the same directory and it has the same problem, so, I suppose the problem isn't the file. The problem is the configuration for the whole directory.
    I just went to export and then selected Runnable jar. That's all.

    Well, the next question is: "Where can I change this configuration?" I've seen all the menus of STS, including the floating mouse-right-button menu over the project, the pom.xml, the myfile.xml and I can't find it. In the pom.xml my outputDirectory is ok, but I suppose this affects only to the target directory inside STS.
    I'm really sorry. I've always worked with NetBeans and this is my begining with eclipse (STS) and Spring.

    Thanks in advance again.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Maven should already do this for you, I assume you are using maven to create your jar and not STS? As mentioned it is the default!
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Feb 2012
    Posts
    7

    Default

    Well, I'm not sure, but I'm afraid not.

    I generate the jar from the menu -> export... -> jar -> executable jar (I'm not in the PC where I'm developing and I'm not sure about the last step). I suppose this means STS is generating the jar, no Maven.

    I'm sure the packaging element of Maven is set to jar. How can I generate the jar from Maven instead of STS?

    Thanks again.
    Last edited by Wessiri; Feb 23rd, 2012 at 05:05 AM.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    If you have a maven project you should use maven to generate your jar, ie mvn clean install or package...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Feb 2012
    Posts
    7

    Default

    Well.
    - I've look for a maven console everywhere in STS. I've found one but it seems it doesn't work (I think it's a output console).
    - So I've installed OpenInTerminal.
    - It didn't work because it didn't find the xterm file.
    - I've change the path to xterm.
    - I've tried mvn --version and it hadn't worked.
    - I've set the OpenInTerminal to export M2_HOME, and M2 and the path.
    - Finally it has worked.
    - I've generate the jar and myfile.xml is ok.
    - But there's no library at all and I get an error that says "no hay ningun atributo de manifiesto principal en myfile.jar". The translation from spanish is something like "There isn't any main attribute of the manifest in myfile.jar". I've checked I have a /META-INF/MANIFEST.MF
    - Inside this file is:
    Manifest-Version: 1.0
    Archiver-Version: Plexus Archiver
    Created-By: Apache Maven
    Built-By: wessiri
    Build-Jdk: 1.6.0_26
    - I've read the documentation and I think I need the Main-Class attribute. So, tomorrow I'll try to guess how to configure the manifest file.

    I'm really tired. Everybody that wants to get a jar file from STS with Maven have to do all that things?. As you can see I really try to resolve the problems before I ask anything to a forum but I really think this is too much. With Netbeans everything worked at the first time. I know it my first time with STS, my first time with Maven and my first time with spring but everything is difficult and no intuitive. I'm really tired. It must be a easier way of doing this. I hope so.

    Thanks again for your patiente and your help.

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    In general running maven from STS isn't recommended (at least not for deploying and building wars). But if I recall you can right click the project (or pom.xml) select maven, and there should be an option to select to run a target.

    You want to simply do mvn clean install from the prompt (also the stuff about the manifest shouldn't be needed so I think there is something wrong with your maven installation). You should only need to install maven and that is it, also why on earth are you installing all those command line things your machine already comes with one.

    If you don't have maven installed STS (or actually the M2E plugin!) uses some internal maven which is n't available from the command line.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Feb 2012
    Posts
    7

    Default

    In general running maven from STS isn't recommended (at least not for deploying and building wars).
    I used Maven because some of the tutorials of the dashboard come with Maven, and I must recognize Maven is very very easy to use (I mean about libraries configuration).
    But if I recall you can right click the project (or pom.xml) select maven, and there should be an option to select to run a target.
    I've tried. The Maven menu over the project has: "Add Dependency", "Add Plugin", "New Maven Module Project", // "Download JavaDoc", "Download sources", "Update dependencies", "Update project configuration", // "Disable workspace resolution" and "Disable maven nature".
    Over the pom.xml has only the first three menus. As you can see, useless.


    You want to simply do mvn clean install from the prompt (also the stuff about the manifest shouldn't be needed so I think there is something wrong with your maven installation).
    I only want to make mvn package. But it doesn't work. I'm using the Maven that come with STS. Embedded.
    You should only need to install maven and that is it, also why on earth are you installing all those command line things your machine already comes with one.
    If I can't get a maven console, I thought that OpenInTerminal world come with everything configured, but all I got was a terminal inside STS, but without any configuration.

    If you don't have maven installed STS (or actually the M2E plugin!) uses some internal maven which is n't available from the command line.
    STS and maven installed.

    Anyway, two things. 1) I'm deleting everything (but my java files) and reinstalling everything (STS and so on...) 2) Thanks a lot (sincerily) for all your help. I know that this isn't perhaps the right forum for my question but I was desperate and I thought this was the right place for STS duties.

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default


    I used Maven because some of the tutorials of the dashboard come with Maven, and I must recognize Maven is very very easy to use (I mean about libraries configuration).
    I NOWHERE mentioned that you shouldn't use maven I only mentioned that you should use maven for certain targets (or very custom builds) from the IDE you should then run maven from the command line. I find maven (or any other dependency management build tool) inevitable these days.

    You should install maven and if you want to run maven from the command line use that the embedded maven which comes with the M2E plugin is only usable by the plugin not as an external tool.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

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