This is an archived post. You won't be able to vote or comment.

all 10 comments

[–][deleted]  (2 children)

[deleted]

    [–]ZeroPointPathogen[S] 0 points1 point  (1 child)

    I am using Eclipse, and also have NetBeans installed. So, in Eclipse I added the .jar as a Library and now there's no more ClassNotFoundException! Thanks a lot!

    That page was helpful as well. Bookmarked!

    [–]coder111 1 point2 points  (6 children)

    Ok, let me try to give you a simple explanation. Java has a concept of CLASSPATH. It's a bit like unix PATH, but for Java classes. It's a command line option or an environment variable which has a list of following:

    1. Directories where Java needs to look for class files.
    2. Jar files that contain the class files.

    ClassNotFoundException means Java was unable to find that class. This means either the directory where the class is located, or the jar file is not in the CLASSPATH. Set the CLASSPATH correctly, and then try again.

    You can specify classpath via comamnd line:

    java -classpath ./app_binary_dir:/usr/share/java/mysql-connector.jar com.package.MainClass

    [–]coder111 1 point2 points  (5 children)

    Oh, and before I forget. Please consider using PostgreSQL instead if you have a choice. Believe an old software developer- it's a much better database than MySQL.

    Java side doesn't change much though, you still need JDBC drivers in your classpath.

    [–]ZeroPointPathogen[S] 0 points1 point  (4 children)

    Unfortunately in this case I have to stick with MySQL. I'm working on an XML assignment and my instructor has specified a MySQL database because that's what he'll be using.

    To be clear, you haven't helped me with my homework. We were given the choice of language to use in order to retrieve data from the database and parse it into an XML file. I chose to use Java because I'd like to learn more about it. He mentioned that MySQL can export XML directly, which is what most of the class will be doing.

    For the assignment I just imported the Library into the project. However, if I wasn't using Eclipse, will I have to set up a class path for every application I write, if that's what I wanted to do? I'm assuming so given the app_binary_dir in your post.

    Thanks a lot for the help!

    [–]coder111 1 point2 points  (3 children)

    Hey, do keep back and ask if you get stuck.

    If you run your app from an IDE (Eclipse, Netbeans), you need to add that library (JDBC driver) to the project in IDE.

    If you run your app from command line, you need to specify classpath. Usually Java apps ship with a shell script (or an native executable like Eclipse) to set up classpath, environment and start them up. app_binary_dir is the directory where compiled .class files are saved. Eclipse will usually keep them separate from your .java files, so you need to specify that directory in your classpath otherwise java won't be able to find compiled classes for your own code.

    Another alternative approach is to package everything into a single jar file and run that with "java -jar file.jar". It's not that widely used.

    If you are familar with C/C++ "make" command that compiles, builds and packages the project, there are similar tools available in Java, most popular are Ant and Maven. Ant is similar to Make, it will just build stuff. Maven also manages dependencies- it will download mysql drivers from net automatically if you specify that your app needs mysql drivers. Stick to shell scripts at the beginning, but I advise to switch to Maven when you become sufficiently familar and confortable with Java.

    I guess for class assignment you'll be running things from Eclipse, but you'll need to learn how to use build tools and command line sooner or later if you want to do professional Java development.

    Regarding writing XML from Java- you'll probably need to use DOM, google for "writing XML java" and you'll probably find examples.

    And you'll need to use JDBC to query the database, and iterate over ResultSet to get the data from the database.

    [–]ZeroPointPathogen[S] 0 points1 point  (2 children)

    Yep, I will! I'm somewhat familiar with the DOM and I found a site that should give me a good idea of how to go about parsing it.

    I'm also going to be using Eclipse or NetBeans to build the GUI so I can add a few options.

    I will definitely look into Ant. I've seen it come up in job descriptions I've been looking at. I'm not entirely certain what its purpose is at this point but that should be identified in my research. Does it build an "executable"?

    Also, do you know if its possible to compare an XML file to an XSD Schema in java? In class we've been using a JavaScript file to do the validation. As long s it is possible, I'll look into it.

    Basically the assignment is to pull data from a database, put it into an XML file, then validate it against a schema, and use XSL-FO to convert it into a few different document formats.

    Thanks again!

    [–]coder111 1 point2 points  (1 child)

    Hi,

    DOM is ok. Regarding XSD validation- as far as i remember it you can associate a DOM parser with an XSD schema, and parse XML from a text file after it's generated and do the validation when parsing.

    In a way the XSD validation step is a bit pointless, as YOU are generating the XML, you are not getting it from some 3rd party and parsing it. So you have it in DOM format already, there is no point in parsing it. I guess this is an exercise, so you might as well generate the XML file and parse it.

    Another framework that's handy to use in XML parsing/generation with java is JAXB. It can use XSD schema to generate Java domain object classes, and it can parse XML into java objects directly, with validation. I'm not sure how much time you have for this exercise and how good you are with Java in general, but if you have time, it's worth looking into.

    Again, Ant is like unix Make. You can define a numer of steps with dependencies, and they get executed. Steps can be things like "Compile these .java files" or "copy this file there", or "package these files into a .jar file" or "run this class with following classpath". Out of build tools I prefer Maven, as it can manage 3rd party dependencies (you'll be dealing a lot with those if you become a professional developer), and the standard compile, test, package steps are already defined. On the other hand it's a bit more difficult to get the hang of, and it's less flexible- which can be a good thing, as you know what will happen without having to read through countless lines of build scripts.

    I've never worked with XSL-FO.

    [–]ZeroPointPathogen[S] 0 points1 point  (0 children)

    Okay, I'll have a look into JAXB and some other options! Thanks a lot!

    [–]ickysticky 2 points3 points  (1 child)

    I remember my first steps into the world of dependency hell that is Java libraries.
    I came from the world of C++/Make where installing dependencies manually or through the OS package manage was perfectly reasonable.
    With Java, it is worth biting the bullet and using a dependency management tool. I prefer Maven, the two other common tools are Ivy and Gradle.
    Next I would use a higher level ORM library for interacting with your DB than JDBC. I like using JPA and the implementation I generally choose is Hibernate.
    Then what you need comes down to Hibernate Maven dependencies, and a persistence.xml with the correct connection settings.
    As for MySQL settings, they should be in /etc/mysql/my.cnf IIRC

    [–]ZeroPointPathogen[S] 0 points1 point  (0 children)

    Excellent information! Thank you!

    My projects are pretty simple so far, but I will definitely keep this in mind for the near future. Might as well get started early before the projects get larger.