Results 1 to 20 of 58
- 08-24-2011, 04:48 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 25
- Rep Power
- 0
[MySQL][Jar] Problems outside the IDE
My code works very well on NetBeans but I can't run the .jar file in command line, I received these errors:
Exception in thread "main" java.lang.NullPointerException
at cadastro.dao.DadosDao.findDados(DadosDao.java:44)
at cadastro.controller.DadosController.listaDados(Dad osController.java:68)
at cadastro.Principal.<init>(Principal.java:33)
at cadastro.Cadastro.main(Cadastro.java:12)
These are the codes near the errors
DadosDao (findDados around line 44)
DadosController( listaDados, around line 68)Java Code:public List<Dados> findDados() throws SQLException { List<Dados> dadoss = new ArrayList<Dados>(); String select = "SELECT * FROM dados"; PreparedStatement stmt = getConnection().prepareStatement(select); //Line 44 ResultSet rs = stmt.executeQuery();
Principal (around line 33):Java Code:public List<Dados> listaDados() { DadosDao dao = new DadosDao(); try { return dao.findDados(); //Line 68 } catch (SQLException e) { JOptionPane.showMessageDialog(null, "Problemas ao localizar contato\n" + e.getLocalizedMessage()); } return null; }
Java Code:private JFileChooser flImagem; private JButton btnSalvar, btnAlterar, btnExcluir, btnClear, btnLocalizar; private JButton btnPrimeiro, btnProximo, btnAnterior, btnUltimo, btnAbrir; private JButton btnCarregar; private List<Dados> contatoList = new DadosController().listaDados(); //Line 33 private int registroAtual = 0;
- 08-24-2011, 05:03 PM #2
How are you running the jar from the command line, what are you typing in?
If its runnning ok in the IDE, could be a classpath issue. Check the manifest file and see what the path is to the class that has your main method. Probably something like packagename.className. Then try to run it using "java -cp packagename.classname MyJarFile.jar
- 08-24-2011, 05:07 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 25
- Rep Power
- 0
I'm doing this
java -jar "/home/lsa/NetBeansProjects/Cadastro/dist/Cadastro.jar" (suggested by the IDE)
I tried the method you just said and got theses errors
Exception in thread "main" java.lang.NoClassDefFoundError: Cadastro.jar
at gnu.java.lang.MainThread.run(libgcj.so.11)
Caused by: java.lang.ClassNotFoundException: Cadastro.jar not found in gnu.gcj.runtime.SystemClassLoader{urls=[], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.11)
at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj .so.11)
at java.lang.ClassLoader.loadClass(libgcj.so.11)
at java.lang.ClassLoader.loadClass(libgcj.so.11)
at gnu.java.lang.MainThread.run(libgcj.so.11)
- 08-24-2011, 05:36 PM #4
The error you get on line 44 could be caused by getConnection() returning a null value.
Break line 44 into separate steps and print out the value returned by getConnection() to see if it is null.
The instructions in post#2 are not going to help. You are executing the code ok, but are having a problem there.
The instructions from post#2 should say something like this:
java -cp MyJarFile.jar;. cadastro.Cadastro
- 08-24-2011, 06:17 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
What Norm said.
I would lay good odds that your getConnection() is actually eating an exception and returning a null Connection.
That exception is quite likely to be a ClassNotFoundException (the MySQL connector jar isn't where it should be), or a SQLException (failed to connect).
- 09-06-2011, 12:49 PM #6
Member
- Join Date
- Aug 2011
- Posts
- 25
- Rep Power
- 0
Re: [MySQL][Jar] Problems outside the IDE
the value of getConnection is right, at least on the IDE, it returns the value
com.mysql.jdbc.JDBC4Connection@19fcc69
I really don't know what else to do...
- 09-06-2011, 01:00 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: [MySQL][Jar] Problems outside the IDE
Then the above code is not the source of that errror.
Line 44 only has one thing on it that can be the cause of your NPE and that is the return value of getConnection() has to be null.
Since you haven't shown us your getConnection() code we can't say why.
Rewrite the top method as:
and check what the value of connection is.Java Code:public List<Dados> findDados() throws SQLException { List<Dados> dadoss = new ArrayList<Dados>(); String select = "SELECT * FROM dados"; Connection connection = getConnection(); System.out.println(connection); PreparedStatement stmt = connection.prepareStatement(select); ResultSet rs = stmt.executeQuery();
- 09-06-2011, 01:16 PM #8
Member
- Join Date
- Aug 2011
- Posts
- 25
- Rep Power
- 0
Re: [MySQL][Jar] Problems outside the IDE
The value is "com.mysql.jdbc.JDBC4Connection@253498"
The code for getConnection is here
Java Code:public static Connection getConnection() { System.out.println("Conectando ao Banco de Dados"); try { //Carrega o Driver do Banco Class.forName(DRIVER_CLASS); Connection conn = DriverManager.getConnection(URL_MYSQL, USER, PASS); if (conn != null) { System.out.println("STATUS--->Conectado com sucesso!"); System.out.println("conn =" +conn); } else { System.out.println("STATUS--->Não foi possivel realizar conexão"); } return conn; } catch (ClassNotFoundException e) { System.out.println("O driver expecificado nao foi encontrado."); //e.printStackTrace(); } catch (SQLException e) { System.out.println("Nao foi possivel conectar ao Banco de Dados."); //throw new RuntimeException(e); return null; } return null; } }
- 09-06-2011, 01:28 PM #9
Re: [MySQL][Jar] Problems outside the IDE
What is the text of the error messaged now?
- 09-06-2011, 01:36 PM #10
Member
- Join Date
- Aug 2011
- Posts
- 25
- Rep Power
- 0
Re: [MySQL][Jar] Problems outside the IDE
The same message as before
- 09-06-2011, 01:38 PM #11
Member
- Join Date
- Aug 2011
- Posts
- 25
- Rep Power
- 0
Re: [MySQL][Jar] Problems outside the IDE
But outside the IDE it gets the value NULL instead of the value that is used to be....
- 09-06-2011, 01:43 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 09-06-2011, 01:44 PM #13
Re: [MySQL][Jar] Problems outside the IDE
Please post the full text of the error message.
What variable is null? Why is that variable null? Add lots of printlns to show the contents of the variable to see where it is being set to null.
- 09-06-2011, 01:52 PM #14
Member
- Join Date
- Aug 2011
- Posts
- 25
- Rep Power
- 0
- 09-06-2011, 01:59 PM #15
Re: [MySQL][Jar] Problems outside the IDE
That would be because of the missing class definitions that are in the missing jar file.
You need to add the jar file to the classpath for the java program.
How do you execute your program outside of the IDE?
- 09-06-2011, 01:59 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: [MySQL][Jar] Problems outside the IDE
This is your bug (as I said earlier).Java Code:catch (ClassNotFoundException e) { System.out.println("O driver expecificado nao foi encontrado."); //e.printStackTrace(); } catch (SQLException e) { System.out.println("Nao foi possivel conectar ao Banco de Dados."); //throw new RuntimeException(e); return null; } return null; }
You are eating your exceptions.
You are also treating the failure to actually get a connection (which is the purpose of this method) as a perfectly reasonable event. It isn't. It is a failure in your system and should be throwing an exception, not returning null.
- 09-06-2011, 02:07 PM #17
Member
- Join Date
- Aug 2011
- Posts
- 25
- Rep Power
- 0
Re: [MySQL][Jar] Problems outside the IDE
Norm
I'm using this to run the .jar file
java -jar "/home/lsa/NetBeansProjects/Cadastro/dist/Cadastro.jar"
How do I add the jar file to the classpath?
Tolls
With those exception I can see many errors, like :
Exception in thread "main" java.lang.RuntimeException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorExceptio n: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '????????????????' at line 1
at cadastro.dao.ConnectionDataBase.getConnection(Conn ectionDataBase.java:49)
at cadastro.dao.GenericDao.<init>(GenericDao.java:19)
at cadastro.dao.DadosDao.<init>(DadosDao.java:16)
at cadastro.controller.DadosController.listaDados(Dad osController.java:65)
at cadastro.Principal.<init>(Principal.java:33)
at cadastro.Cadastro.main(Cadastro.java:12)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorExceptio n: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '????????????????' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1049)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:19 90)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionIm pl.java:2619)
at com.mysql.jdbc.ConnectionImpl.configureClientChara cterSet(ConnectionImpl.java:1881)
at com.mysql.jdbc.ConnectionImpl.initializePropsFromS erver(ConnectionImpl.java:3496)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(Co nnectionImpl.java:2385)
at com.mysql.jdbc.ConnectionImpl.createNewIO(Connecti onImpl.java:2154)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImp l.java:792)
at com.mysql.jdbc.ConnectionImpl.getInstance(Connecti onImpl.java:377)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:305)
at java.sql.DriverManager.getConnection(libgcj.so.11)
at java.sql.DriverManager.getConnection(libgcj.so.11)
at cadastro.dao.ConnectionDataBase.getConnection(Conn ectionDataBase.java:26)
...5 more
But I don't know how to solve any of them, the same errors as before...just with the databases errors now
- 09-06-2011, 02:12 PM #18
Re: [MySQL][Jar] Problems outside the IDE
You can add a Class-path: entry to the manifest file in your jar file.
- 09-06-2011, 02:19 PM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: [MySQL][Jar] Problems outside the IDE
What versions of things do you have?
Java, JDBC driver, MySQL, that sort of thing.
Looks like you have a mismatch.
Or something's up with your username and password, or connection string possibly?
You also need to tell us where you are getting these exceptions, what system, and whether this is via the IDE.
And what the getConnection() code now looks like.
- 09-06-2011, 02:31 PM #20
Member
- Join Date
- Aug 2011
- Posts
- 25
- Rep Power
- 0
Re: [MySQL][Jar] Problems outside the IDE
The code of getConnection()
JDBC driver: 5.1.17Java Code:public class ConnectionDataBase { private static final String URL_MYSQL = "jdbc:mysql://localhost:3306/hospital"; private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver"; private static final String USER = "root"; private static final String PASS = "lsa1234"; public static Connection getConnection() { System.out.println("Conectando ao Banco de Dados"); try { //Carrega o Driver do Banco Class.forName(DRIVER_CLASS); Connection conn = DriverManager.getConnection(URL_MYSQL, USER, PASS); if (conn != null) { System.out.println("STATUS--->Conectado com sucesso!"); } else { System.out.println("STATUS--->Não foi possivel realizar conexão"); } return conn; } catch (ClassNotFoundException e) { System.out.println("O driver expecificado nao foi encontrado."); e.printStackTrace(); } catch (SQLException e) { System.out.println("Nao foi possivel conectar ao Banco de Dados."); throw new RuntimeException(e); } return null; } }
MYSQL: 5.1.54
Java: JDK 6 Update 27 with NetBeans 7.0.1
I'm using Linux Ubuntu 11.04
The password and username are ok!
Similar Threads
-
Problems using MySQL database
By Antrim in forum EclipseReplies: 1Last Post: 02-16-2011, 04:57 AM -
Problems with MySQL Driver
By islan in forum JDBCReplies: 7Last Post: 08-06-2009, 04:47 PM -
MySQL/JDBC Mysql query output
By thelinuxguy in forum Advanced JavaReplies: 4Last Post: 02-13-2009, 01:57 AM -
hello the community & mysql connecting problems
By scchia in forum New To JavaReplies: 6Last Post: 07-16-2008, 08:49 AM -
problems about storing binary data to mysql db using PreparedStatement
By xiechao in forum New To JavaReplies: 0Last Post: 04-22-2008, 11:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks