Announcement Announcement Module
Collapse
No announcement yet.
Problem loading xml file Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 05:30 PM.

  • #2
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        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!

        Comment


        • #5
          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, 05:05 AM.

          Comment


          • #6
            If you have a maven project you should use maven to generate your jar, ie mvn clean install or package...

            Comment


            • #7
              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.

              Comment


              • #8
                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.

                Comment


                • #9
                  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.

                  Comment


                  • #10

                    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.

                    Comment


                    • #11
                      I deeply regret you feel my last post as an attack. You know I've always closed my post with thanks. I only wanted to justify my decision. I didn't NEVER wanted to question your answers. In fact, they helped mi a lot. You know I'm a newbie (I've never hidden it). I can only be grateful for your help.

                      I must say I've got what I wanted. I followed your advise and I reinstalled STS (it was something wrong with the old one). After this, I found out Run as... -> Maven build ... (it was always there but I saw it so far) and I get a JAR with everything. The only problem was the manifest didn't have a MainClass.
                      I find out that I had to configure the maven-jar-plugin but how? Finally I used the Maven -> Add plugin with the right click over the pom.xml. With this I could configure the manifest, but then the jar didn't load all the dependencies.
                      At least I found the maven-shade-plugin that is ready for runnable jars and everything is OK now.

                      Thanks again for all your help and your time and I beg your perdon for any inconvenience.

                      Comment


                      • #12
                        Not sure where you got that impression from but I didn't feel it as an attack, just wanted to make sure you got my point (probably a bit to much for that).

                        I general I use maven in STS only for managing my dependencies anything else I do from the command line. I simply edit the pom.xml file by hand from within STS, for some reason that gives me a better feeling than the plugin.

                        But I'm glad it worked out (at last). Instead of maven you also might want to take a look at Gradle which should be a bit easier and more flexible than maven.

                        Comment


                        • #13
                          So this is not a problem with DataContractSerializer but a problem with XmlDictionaryReader.CreateTextReader?
                          Last edited by hasselcloud; Mar 5th, 2012, 01:52 PM.

                          Comment


                          • #14
                            I've been reading something about gradle. It seems something easy to use like Maven, but a bit more flexible. It can be interesting. Thanks for your advise. I put in my list of future things to learn, with Hibernate, Hudson, and a lot of more things. Now I think it's enough with STS, Spring and Maven.
                            Thanks again.

                            Comment

                            Working...
                            X