I have developed a program that pulls the data from a remote sql server and pushes it to local mysql server .I have a jar file exported for this .
Now I am trying to run this jar file on different machine to do the same task .All the IP's and ports have been changed accordingly .There is no IDE on this machine .
When i am running this jar file , i get an error saying "No suitable driver found for jdbc:sqlserver:/ 172 .* "
I have installed the jdbc driver in program files .
So what do i have to do to run this jar file .
You need to add the library to the runtime classpath. A straightforward way would be to export it as a dependency in the jar (although there might be licencing issues with that).
How are you creating the jar?
Add the jdbc driver in your classpath, not in your "program files".... Check the documentation of the JVM http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html
All you need to run a Java program is a Java executable and class files or zipped/jared class files and other files needed by class files. So 1st step is locate your java executable, maybe c:\java.exe or /home/yourhome/java, and 2nd step is gather all your classes or zip/jar archive and put them in a String split by ':' on Unix or Unix-like or ';' on windows. 3rd step is to identify your main class, which is the only entry of your program.
In the end, use YOUR_JAVA_EXECYTABLE -classpath YOUR_CLASS_PATH YOUR_MAIN_CLASS. works fine for most scenarios.
Forget anything about "installed" a program or driver, which is just put some some in system's classpath or path or anything like this.
Related
I've installed zookeeper v 3.4.9 on suse using the following package:
http://download.opensuse.org/repositories/home:/nicolasbock:/midokura/openSUSE_Leap_42.2/x86_64/zookeeper-3.4.9-1.1.x86_64.rpm
Starting the server works just fine an the various application that need to connect to it seem to be able to do so just fine, however I'm getting a strange error when I try to start the client:
zkCli.sh -server 127.0.0.1:2181
Error: Could not find or load main class org.apache.zookeeper.ZooKeeperMain
I've tried searching around the internet but the only answer I've come up with is setting the env variable $CLASSPATH. I tried looking what classpath is used internally by the serve starting script and set $CLASSPATH to that but with no results.
I'm running openjvm v 1.8.0_121
I encountered this problem today on Ubuntu 18.04. On zookeper official download page once one follows the given mirror link and chooses stable version, he can see 2 files available. In my case those were:
apache-zookeeper-3.5.5-bin.tar.gz
apache-zookeeper-3.5.5.tar.gz
I chose apache-zookeeper-3.5.5.tar.gz, observed the same problem. Tried to print CLASSPATH as #Jonathan suggested, it was printing different paths however, problem is that the archive file did not contain zookeper jars, probably it is expected that user packages to jar himself as I could find source code in that archive file.
Downloading larger apache-zookeeper-3.5.5-bin.tar.gz archive, extracting and running ./zkCli.sh went smooth as it had everything necessary, so whoever faces the same problem, try that and see if it does the trick for you.
Are you trying on Unix or Windows?
I dont think, you need "-server 127.0.0.1:2181"
Just run zkCli.sh or zkCli.cmd
That is a classpath problem. The zkCli.sh script is attempting to launch a java app where the main class is ZooKeeperMain. However, java cannot find the ZooKeeperMain class file because the classpath being passed to java does not contain a jar file that contains the ZooKeeperMain class.
Typically, the ZooKeeperMain.class file is contained in a jar file at something like $ZOOKEEPER_HOME/zookeeper-3.4.13.jar.
You might trying modifying the zkCli.sh script to print out the classpath just before the script attempts to launch ZooKeeperMain. The classpath should contain the jar that contains the ZooKeeperMain class.
On my Windows 10 machine, my classpath looks like this:
/c/Program Files (x86)/Zookeeper/bin/../build/classes:/c/Program Files (x86)/Zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/c/Program Files (x86)/Zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/c/Program Files (x86)/Zookeeper/bin/../lib/netty-3.10.6.Final.jar:/c/Program Files (x86)/Zookeeper/bin/../lib/log4j-1.2.17.jar:/c/Program Files (x86)/Zookeeper/bin/../lib/jline-0.9.94.jar:/c/Program Files (x86)/Zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/c/Program Files (x86)/Zookeeper/bin/../zookeeper-3.4.12.jar:/c/Program Files (x86)/Zookeeper/bin/../conf:
I encountered a similar errors as yours when launching zkCli.sh from Git Bash for Windows on my Windows 10 machine. My issue was because of the way Git Bash was handling wildcards in the classpath. I initially had some *.jar entries in my classpath that I removed to get zkCli.sh to launch.
I have a project which uses both RMI and JDBC and I need to make it an executable JAR. Now my instructions are to make one JAR file with all my source code which ive included all my .java files. My second JAR needs to be the RMI server .class files and then the third JAR is to have all my RMI client .class files.
Now the first JAR and the third JAR I can make no problem but the issue lies with that in my second I require the use of JDBC so I need to include the mysql-connector-java-5.1.31-bin.jar file in it.
My folder consists of the following layout, only default packages are used and everything is in the root directory.
1st JAR - Source Code Jar File Contains...
A2Interface.java
A2InterfaceImpl.java
A2RmiClient.java
A2RmiServer.java
InvalidLocationException.java
DuplicatedAddressException.java
I used this command to jar it
jar cvf source.jar *.java
2nd JAR - Contains the classes related to RMI server, I also need the mysql JDBC jar file included along with it
And I used this command to JAR it
jar cvfm RMIserver.jar server.txt A2RmiServer.class A2Interface.class
A2InterfaceImpl.class *Exception.class mysql-connector-java-5.1.31-bin.jar
My server.txt Manifest file contains the following
Main-Class: A2RmiServer
Class-Path: mysql-connector-java-5.1.31-bin.jar
-empty line as per the docs-
3rd JAR - Contains the classes related to the RMI client
jar cvfm RMIclient.jar client.txt A2RmiClient.class
A2RmiClient$EventHandler.class
Any my client.txt manifest file contains
Main-Class: A2RmiClient
-empty line as per the docs-
Now everything jars perfect fine and I extract my first JAR file containing all my .java files with no errors. I then attempt to run my RMIserver.jar file with the following command...
First I start the registry..
start rmiregistry 5566
Then I run the executable jar file..
java -cp mysql-connector-java-5.1.31-bin.jar -jar RMIserver.jar
AND here is where I get an Exception of the following
Trouble: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: A2Interface
I cannot determine why this is doing this at all. If I dont use any jar files my code runs perfectly fine with ZERO exceptions but as soon as I try this I always get exceptions. I have searched many places and some people say that you cant include the mysql JAR file (or any JAR file) like this and even according to the Java doc here http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
They also mention that its designed for use only for JAR's over the network and not in the same directory, however my professor believes this can be done. I have been trying to figure this out for hours and have come to a wall. I don't know how I can make it work with using the necessary mysql JAR file.
OH and please dont mention using any other tools to create a JAR package as I cannot do that for my assignment or even eclipse. I can only do this from using strictly command line tools.
The ClassNotFoundException is happening in the rmiregistry, and it's being returned to your server application wrapped in a ServerException.
In normal usage, the RMI server annotates the classes with their codebase URL so that other Java processes can load the unknown classes. However, the jar file isn't a usable URL for this purpose (I'm not totally sure, but I think it's because it's generally insecure to load from file: URLs received from the network).
The reason it works when you're not using jar files is because the rmiregistry is able to find the missing classes in its own classpath (because you're running it in the same directory that contains A2Interface.class).
You have two choices:
Serve the class files (or a jar containing them) from an http server running on the RMI server's host, and use the java flag -Djava.rmi.server.codebase=http:... to point to the directory/jar where the classes can be found; or
Insert the common classes in every classpath: server, client, and rmiregistry.
For more detail, see http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/codebase.html.
And here is where I get an Exception of the following
The Registry doesn't have that class available on its CLASSPATH. There are three solutions, in increasing order of difficulty:
Run the Registry inside the server JVM, via LocateRegistry.createRegistry(). When you do this, you must store the result of that call in a static variable, to prevent it being garbage-collected. That also makes it possible to unexport the Registry when you want the JVM to exit.
Run the rmiregistry with an appopriate CLASSPATH. There are several ways to do that, including -J-classpath and setting a CLASSPATH environment variable.
Use the RMI codebase feature.
Now my instructions are to make one JAR file with all my source code which ive included all my .java files. My second JAR needs to be the RMI server .class files and then the third JAR is to have all my RMI client .class files.
Your instructions are incorrect. You need to make four JAR files:
Source code (why?)
.class files that are common to both client and server
.class files that are only used on the server
.class files that are only used on the client.
(3) and (4) need manifests, both to name the Main-Class and to name (2) on the Class-path.
The server deployment needs (2) and (3). The client deployment needs (2) and (4).
I have been following this tutorial accordingly.
Up to running the FirstExample class in the command prompt is when it starts to freak out for some reason. After attempting to run the following command:
java FirstExample
I get the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: FirstExample
I understand that it can't find the FirstExample class due to the classpath (for some reason) so I executed the following command:
java -cp . FirstExample
And now it returns a new exception:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Now it can't find the JDBC Driver. This confuses me because for starters, I ran the exact same coding through Eclipse and it works as expected, and secondly, I went as far as to ensure that I execute the same class file that Eclipse is executing, and the command prompt still returns exceptions. I also went as far as to put the FirstExample file in a separate folder, just for the purpose of copying and pasting the MySQL Connector into the same folder, and I still get exceptions.
I just don't understand whats going on, can someone help me please?
Many thanks.
The file path to the connector is as followed:
C:\Program Files\MySQL\mysql-connector-java-3.1.14\mysql-connector-java-3.1.14-bin.jar
Hope this helps.
For testing purposes, I have placed the FirstExample class under the following path:
C:\java
This confuses me because for starters, I ran the exact same coding through Eclipse and it works as expected
This is because in Eclipse you add the libraries to the Build Path, and it will use all the libraries specified there in the classpath automatically when running your project. This can be noted here:
In order for you to execute your project using third party libraries from command line tools, you should specify the libraries manually in your classpath explicitly:
java -cp <path/to/mysql_jar/goes/here>:. FirstExample
By your comment:
the path to the MySQL file is: C:\Program Files\MySQL\mysql-connector-java-3.1.14\mysql-connector-java-3.1.14-bin.jar (...) I have placed the FirstExample class under C:\java
This should be the command line to use:
java -cp "C:\Program Files\MySQL\mysql-connector-java-3.1.14\mysql-connector-java-3.1.14-bin.jar; ." FirstExample
Note that it is better to store all the third party libraries in a single folder within your project, usually called lib, and put a reference to there. Assuming your current folder has a lib folder and all the third party libraries are copied there, the command line would be:
java -cp "lib\*; ." FirstExample
Use the next example to add your jars to the classpath:
java -cp "jdbc.jar;lib/*" my.package.FirstExample
You need to have the class com.mysql.jdbc.Driver (and all the imported classes) in the classpath too.
You should download the jar (http://dev.mysql.com/downloads/connector/j/5.0.html) and add it to the classpath.
The ClassNotFound Exception rises when there is an issue with the class name that you have written in Class.forName() or if the package is not set to the classpath variable. Make sure that you have added the jar file to the classpath ( C:............\jarfilename.jar;).
This is applicable for any JDBC Driver and jar files. The .jar files that are added to the classpath will not be visible to IDEs, in this case, you need to add the jar files to buildpath (in eclipse) or you can also copy the jar files to ext folder available in the Java installation folder.
Also note that the jar files of the DB Softwares may vary based on the DB software version that you are using for example if you are using the Oracle 11g, you need ojdbc6.jar file, in other versions of Oracle the number changes like ojdbc14.jar etc.
I am fairly new to Linux, I am trying to set my jar files for OJDBC in my classpath but so far have not found any good examples for a beginner such as me.
I will list the my paths for Jar file below. Can someone provide me with example or how to step by step set my class path with the commands. I would really appreciate it. I would like to set this path in my project which I am using it in.
Jar file:
/home/ssingh/Downloads/oracle.jar
Project:
/opt/savi/Workspace/SgRecc/bin/PerVal.class
Setting up the Class Path
To use the driver, the JAR archive named postgresql.jar if you built from source, otherwise it will likely be (named with the following convention: postgresql-[server version].*[build number].jdbc[JDBC version]*.jar, for example postgresql-8.0-310.jdbc3.jar) needs to be included in the class path, either by putting it in the CLASSPATH environment variable, or by using flags on the java command line.
For instance, assume we have an application that uses the JDBC driver to access a database, and that application is installed as /usr/local/lib/myapp.jar. The PostgreSQLâ„¢ JDBC driver installed as /usr/local/pgsql/share/java/postgresql.jar. To run the application, we would use:
export CLASSPATH=/usr/local/lib/myapp.jar:/usr/local/pgsql/share/java/postgresql.jar:.
java MyApp
I am really new to database and this is my first program in database using java Netbeans 7.1 --- It is summer on our country now and I am a student with the course I.T. Our next subject on programming is about database and since there's no class I spend my time learning database for preparation for the next school semester and I refer to this site as my guide for the first database program I currently making now.
http://www.homeandlearn.co.uk/java/databases_and_java_forms.html
I did everything in the tutorial and I actually done doing the program.
The final thing I did is I clean and build the program since I want the program to run with out opening the netbeans again I downloaded the JRE and make my database_form.jar as a jar executable. "database_form" the name of my Netbeans Project. I do that by making javaw in JRE.7 as my dafault when opening any jar files.
Anyway, this is how i run the program.
Running Program in Netbeans IDE
Using Netbeans, before my program works I first need to "Start Server" on JavaDB. because if I didn't do that an Exception occurred "Err. connecting to server localhost 1527 and so on"
Running Program using jar executable alone.
The Problem is there's an Exception and Err in connecting still occurred.
What I want to achieve?
I want the program to run without opening the Netbeans IDE and going to Java DB to click the "Start Server", I dont want to do that anymore. Or my second option is to start the server using command prompt so that I just have to make a bat file so that whenever I open my program database_form.jar I just place the bat file on my desktop and run it.
Second Problem!
Actually, I already try my second option by using command prompt to start the server but I forgot how I did it, I just found it on some website the only thing I remember is the Exception says "Failed to lunch the server because the database Employees is missing. Employees is the name of my created database.
The O.S. I am using is Windows 7.
Thank you for all the reply and sorry for the long text I just want to be specific, :D
Right, from your description there seems to be a couple of things you are confusing.
First, databases are typically run as servers with multiple clients connecting to them thus allowing they contain to be shared. When you start Java DB you are starting the Java DB database server.
That said, lightweight databases, such as Java DB can be run in an embedded mode as explained here. Remember that the directory you point to with the derby.system.home property will need to contain the database files, if not you'll need to create that programatically too.
Second, there's various ways to run a Java application outside of an IDE, but jars themselves are not executable in the same way an exe file is in Windows.
The simplest way is to call the java executable passing the necessary classpath and the name of the class containing the main method. For example if I had a class called com.example.Application that had been compiled to a directory C:\dev\example\classes the following command line would run the application:
java -cp C:\dev\example\classes com.example.Application
If there were dependencies on external libraries, as there will be in your case on the Derby JDBC driver, then those would also need including in the classpath resulting in something like:
java -cp C:\dev\example\classes;C:\dev\lib\derby.jar com.example.Application
There's a full set of document on the Java launcher here.
Now, back to the jar. Like I said, jars are not executable but there is something that's know as an 'executable jar'. This is the same as any jar except there are some special additions to the manifest to specify the application entry point or main-class and the class-path as described here.
Once the main-class and class-path are specified in the jar's manifest, the following command line would run the application:
java -jar C:\dev\example.jar
You can even associate the jar extension with the java exe and double clicking the jar will cause the application to launch (though on a dev machine it's probably more useful that the jar extension be associated with WinZip or simlar in order to open that jar).
The default database in Netbeans is Derby/JavaDB. So you need to:
add the jar of javadb/derby in our classpath (it maybe already present, as it is bundled with java when you install it in Ubuntu)
setup a path with the jdbc URI to save the database data
I personally recommend the usage of hsqldb or H2 for this: they support in-memory database, very useful for stand alone project with no persistence data or for tests.
If you use window, add ODBC Data Sources from Administrative Tools to your Java Derby driver and run jar.