0

I just export my java project to executable jar. I have somehow made my project work to access value from DB (hibernate.cfg.xml), config.properties, and log4j.properties & editable for future...

I want to put hibernate.cf.xml, config.properties and log4j.properties in the outside of jar and place them somewhere in other directory.

I've been search for this and got this way :

  1. Created a /path/to/mydir directory at some place in my deployement system.
  2. Moved log4j.properties and META-INF and hibernate.properties and hibernate.cfg.xml to mydir.
  3. and use this command

    this is just example: java -cp Myjar.jar:/path/to/mydir MyMainClass

    the command that I write is like this: java -cp coreservice.jar:/mon/properties CoreServiceController.java

I've try like that way but i got this error

Error: Could not find or load main class

anyone can help me to fix it or give the better way will be pleasure ~

====================(EDIT)===================================================

Oh my god ~ I have delete(cut) folder META-INF from my jar and paste to my config folder (/mon/properties)

and try this command again java -cp coreservice.jar;mon/properties/ id.co.bn i.coreservice.controller.CoreServiceController..... annnndddddd this work ;))

tha-nks for Apostolos, Antoniossss, Yasa and no name :)

splatter_fadli
  • 761
  • 4
  • 16
  • 32
  • Where did you execute this command? Are you in current Working folder ?? Try with that – Yasa Oct 21 '13 at 10:02
  • @Yasa honestly, I want to put that file in the outside of my jar... so I can edit later and change the setting... – splatter_fadli Oct 21 '13 at 10:05
  • stupid question but does your MyMainClass has a static void main method? – Apostolos Oct 21 '13 at 10:15
  • Then try that command with MyMainClass.java extensions – Yasa Oct 21 '13 at 10:16
  • @Apostolos thanks :), yapp MyMainClass has a static void main method – splatter_fadli Oct 21 '13 at 10:21
  • @Yasa I did, but I got that error – splatter_fadli Oct 21 '13 at 10:22
  • sorry, didn't see Yasa's answer about the extension. i deleted my answer – Apostolos Oct 21 '13 at 10:28
  • does CoreServiceController belong to a package or is it pacakge-less? – Apostolos Oct 21 '13 at 10:28
  • @Apostolos nevermind :), CoreServiceController is a java file that in the package... i've been try to include the package but still not work well – splatter_fadli Oct 21 '13 at 10:32
  • ok so you need CoreServiceController.class first of all and then if this is in package com.test, then in parent directory of com/test you type java -jar MyJar.jar:. com.test.CoreServiceController. this should work – Apostolos Oct 21 '13 at 10:34
  • why the `:`? I never used it. What does it mean? Assuming that the `/mon/properties/` are your packages and `CoreServiceController` is in them, than execution command should be like `java -cp ./coreservice.jar mon.properies.CoreServiceController` – Antoniossss Oct 21 '13 at 10:35
  • @Antoniossss the : is for setting multiple classpath entries in linux (i think ; is for windows) and if this class is outside this jar, you need to state the classpath directory along with the extra jar(s) :) – Apostolos Oct 21 '13 at 10:38
  • ok I get it. So now what is the `CoreServiceController` package name? It should't be the default one. If it is, than change it to apropriate and lunch using fully qualified class name (eg. package.subpackage.myclass) and without .java at the end. Your error is about not finding your class file, not hibernate configs etc (no entry point right now) – Antoniossss Oct 21 '13 at 10:41
  • @splatter_fadli i'm starting to think that i should't delete my answer :) if this was the issue please let me know so as to undelete my answer and you upvote it :) – Apostolos Oct 21 '13 at 10:56
  • @Antoniossss and Apostolos Yapp, thats my problem.... still got this error `Error: Could not find or load main class` – splatter_fadli Oct 22 '13 at 01:46
  • I must stay that you are doing something very very wrong than because this is the very basics of java runtime. 1) Definetly you have to provide fully qualified class name for java to run, not source file (*.java) – Antoniossss Oct 22 '13 at 06:05

3 Answers3

0

You should not use

java -cp coreservice.jar:/mon/properties CoreServiceController.java

you should put the class file there and try again, not the source java file

if this is in package let's say com.test, then in parent directory of com/test you type

java -cp coreservice.jar:. com.test.CoreServiceController.

this should work

(Edit) to load hibernate.cfg.xml file from specific location, try this method inside your HibernateUtil class:

public static void setConnectionProperties() {
    URL configFileURL = null;

    try {
        configFileURL = URL_OF_YOUR_CFG_XML_FILE;
        configuration = (new Configuration()).configure(configFileURL);
        sessionFactory = configuration.buildSessionFactory();

    } catch (HibernateException e) {
        e.printStackTrace(); 
        System.out.println("Error while initializing hibernate: " + e.getMessage());
    }

}
Apostolos
  • 10,033
  • 5
  • 24
  • 39
  • so you are in directory dir1 lets say and inside this dir1 directory you have subdirectory com/test and inside this com/test subdirectory (dir1/com/test) you have a CoreServiceController.class with package declaration of package com.test and you run this command and outputs the same error? are you sure you're doing sth different? – Apostolos Oct 22 '13 at 05:49
  • Yap, I'm sure... when I write this command `java -cp coreservice.jar:/mon/properties id.co.bni.coreservice.controller.CoreServiceController` I got this error `could not find or load main class` but when i change `:` with `;` I got the error that hibernate.cfg.xml (etc) not found – splatter_fadli Oct 23 '13 at 06:24
  • note: I put all my configuration file (hibernate.cfg.xml, log4j.properties and config.properties) into `/mon/properties`... so my purpose is my command can direct hibernate to look that folder for configuration... – splatter_fadli Oct 23 '13 at 06:27
  • @splatter_fadli ok so you are not on linux environment. remember. classpaths in linux are distinguished by : but at windows by ; – Apostolos Oct 23 '13 at 06:43
  • yap, i just remember by now... so how can my command direct the hibernate to look this folder `/mon/properties` for configuration... any idea? – splatter_fadli Oct 23 '13 at 06:49
  • oh, I am so-rry, I paste the wrong one this is the new one `Initial SessionFactory creation failed.org.hibernate.HibernateException: C:/Users/userxxx/Desktop/mon/properties/hibernate.cfg.xml not found Exception in thread "main" java.lang.ExceptionInInitializerError` – splatter_fadli Oct 23 '13 at 08:20
  • so you mean that you have the cfg.xml in that folder but it still throws a not found exception? that' very weird... – Apostolos Oct 23 '13 at 08:21
  • i've try this method before... your method have similarity from this [tutorial](http://www.mkyong.com/hibernate/how-to-load-hibernate-cfg-xml-from-different-directory/)... this method will work if the directory still in the inside of project – splatter_fadli Oct 23 '13 at 08:27
  • I agree with you, but i can't say anything about why "not found"... because this "not found" problem, I try to tricky this application with call config file with prompt but i got "not found" again :( – splatter_fadli Oct 23 '13 at 08:46
  • great! ok but please upvote my answer for all the effort and help :) cheers! – Apostolos Oct 23 '13 at 09:49
0

If I right understood your question:

When you configure sessionFactory you can write so

new Configuration()
            .configure("/path/to/your/files/hibernate.cfg.xml")
            .buildSessionFactory();

full example

  • 1
    Hibernate checks for hibernate.cfg.xml on the class path so hardcoding path (external or not) is not good idea. Providing proper classpath is more than enough. – Antoniossss Oct 21 '13 at 11:22
  • In this particular case it wont work, because question creator dont know how to execute application, AND leaving-it-be with the proper classpath WHEN he lunch it will do the trick anyway. Question is misleading with this particular case because problem doesn't sit in hibernate config etc. – Antoniossss Oct 21 '13 at 11:25
  • @user2740224 like Antoniossss say, this is not what I mean..... I've try this method, this is just read the configuration from other directory but still must in project folder... – splatter_fadli Oct 22 '13 at 01:33
0

So as you have still the same problem than I must say that you are doing something very very wrong, because this is the very basics of java runtime. Principles:

  1. Definetly you have to provide fully qualified class name for java to run, not source file (*.java) so your call has to be proper.package.CoreServiceController
  2. CoreServiceController cannot be in default page (no package name specified) - if it does, create some package just for test eg. service.core and put CoreServiceController there. As for point 1 - your call will be java -cp coreservice.jar:/mon/properties service.core.CoreServiceController nothing more, nothing else.
  3. Java has nasty behaviour if you provide wrong entry for classpath - it does not say that eg. file myUberProgram.jar does not exists etc. It will just ignore such entry and go on. I had similar issues on Linux with CP - mispelled jar file and "what the hell is going on here?! Why is it not running" - same problem as you have - no class found
  4. Check to be 101% sure, if there is CoreServiceController.class file in your jar file - maybe something wrong is with the packaging, and there is no such file in jar.
  5. Finally, if all previous restrictions are fulfilled and nothing works, last chance for you, is to navigate to change working directory to one that jar file is in (simply go there from command line) and try tu run application without external cp provided. From directory where jar is:
    1. Call java -cp Myjar.jar proper.package.CoreServiceController - should work but next one is 100% checked
    2. Call java -cp ./Myjar.jar proper.package.CoreServiceController - all my applications on remote Linux server are lunched from bash scripts just like that and it always works.

If none of this helped you, than I must say dump your mashine into the ocean because it is cursed or something xD - I just want to say, that I have no other ideas how to help you.

For your practice build simple HelloWorld jar and try to lunch it from command line (but not runnable jar so you will have to provide classpath). Maybe in this way you will se what you did wrong. My thinking is done for today, good day sir!

PS. @splatter_fadli I allowed myself to edit your question becouse as for now it is missguiding - you dont have problem with H configuration, only with running app while multiple entries in class path are provided. Maybe it will be accepted. - Aaaaaand it is done.

Last stand edit: According to this question - QA put classpath entry into commas. Try it out. If Class not found will be gone, than we are half way there. -cp "coreservice.jar:/mon/properties"

Community
  • 1
  • 1
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • I promise to you , i'll dump my pc to the bottom of ocean if i can't fix my problem ;))... I put my configuration file into folder `/mon/properties`... evidently, I got no wrong when declare the package because when I try this `java -cp coreservice.jar;/mon/properties id.co.bni.coreservice.controller.CoreServiceController` or `java -cp coreservice.jar id.co.bni.coreservice.controller.CoreServiceController` the error told me hibernate can't find file config but when I change `;` to `:` I got `class not found`... i think my command still not direct to my configuration file ~ – splatter_fadli Oct 22 '13 at 07:50
  • I've try this `java -cp coreservice.jar /mon/properties id.co.bni.coreservice.controller.CoreServiceController` I got `Class not found .mon.properties` error... – splatter_fadli Oct 22 '13 at 07:58
  • I've try this command `java -cp "coreservice.jar:/mon/properties" id.co.bni.coreservice.controller.CoreServiceController` annnndddd I got "Class not found" error :( – splatter_fadli Oct 22 '13 at 09:02
  • note: I got this method after i've read [this](http://stackoverflow.com/questions/9766410/how-to-write-java-code-to-let-jpa-hibernate-know-where-to-look-for-configuration) – splatter_fadli Oct 22 '13 at 09:36
  • Than as much as I would like to, I don't know how to help you. It just should work. Issue is definetly in providing multiple classpaths. – Antoniossss Oct 22 '13 at 09:38
  • I have just tested multiple classpaths with separator and it works for me. Maybe it is about some other class, not the bootstraping one? – Antoniossss Oct 22 '13 at 09:45
  • I don't thinks so, because I just have one file that contain static void main method... – splatter_fadli Oct 22 '13 at 09:52
  • And you have no stacktrace? Can you provide such? Becouse I get quite large detailed stacktrace when I want to lunch from not existing class. – Antoniossss Oct 22 '13 at 10:00
  • I did, I have stacktrace in my code but when I lunch my jar with prompt the error just show me `Could not found or load main class` thats all or can you suggest me to add stacktrace to analyze not existing class problem?? – splatter_fadli Oct 22 '13 at 10:16
  • I just did - I am suggesting you to add stacktrace (always a good idea if such is available). What do you mean by "having stackrace in my code"? – Antoniossss Oct 22 '13 at 10:23
  • I am so-rry, I got misunderstanding about stacktrace ;))... I just got `could not found or load main class` that's all and nothing more when I launch my jar with prompt... – splatter_fadli Oct 22 '13 at 10:35
  • That is very weird, you should get stacktrace with `NoClassFoundError`. Probably your it is swallowed due to you logger configuration (log4j right?). You must get that stacktrace. – Antoniossss Oct 22 '13 at 10:40
  • the problem is my command is not direct hibernate to look the folder that contain my configuration file, my log4j.properties is in there... so, the log never create ... when I put my log4j.properties in my jar it create the log but log told that hibernate and config.properties not found – splatter_fadli Oct 23 '13 at 04:16
  • So your application does start after all! As I don't trust you now (hah!) please share the log content, stacktrace or anything else you are getting in logs or console. – Antoniossss Oct 23 '13 at 05:35
  • but this is not my purpose, I want to put all my configuration(log4j.properties,hibernate.cfg.xml and config.properties) out of my jar... btw, this the part of my stacktrace when I put log4j.properties in my jar `12:55:29,289 INFO Configuration:2145 - Configuration resource: /hibernate.cfg.xml Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found` – splatter_fadli Oct 23 '13 at 05:59
  • And how did you do that? I know that was about keeping configuration externally, but still but if you have some logs you always should share. Anyway, your app was starting, and you was saying it did not - that was veery misleading. What was the problem than? – Antoniossss Oct 23 '13 at 12:27
  • first, I don't change anything about my code or my command for launch my jar ... I just delete (move/cut) META-INF from my jar into my config file... and when I launch this command again `java -cp coreservice.jar;mon/properties/ id.co.bni.coreservice.controller.CoreServiceController`.... and voila it works bhueheeheh ;)) – splatter_fadli Oct 24 '13 at 01:47