Aplicação .Jar não conecta ao Banco de outra maquina
11 respostas
java
M
MoisesJava
Bom Dia, Tenho um aplicação que rodará em 5 máquinas, a minha será o servidor, ao realizar os teste na minha máquina a conexão funciona normalmente pois estou utilizando o localhost:3306 .
Mas quando altero para o ip da minha maquina e crio o .JAR e executo em outro PC e não dá erro de conexão ao banco.
public static Connection conector() {
java.sql.Connectionconexao=null;Stringdriver="com.mysql.jdbc.Driver";// alterei o localhost:3306 para o IP da minha maquinaStringurl="jdbc:mysql://xx.xx.xx.xx/db_controleci";Stringuser="root";Stringpassword="";try{Class.forName(driver);conexao=DriverManager.getConnection(url,user,password);returnconexao;}catch(Exceptione){System.out.println(e);returnnull;}
Instalei o workbenck na outra maquina e dei o privilegio grant no mysql e consegui configurar o banco remotamente, mas o . JAR não consegui.
Abner bom dia, agora eu coloquei dessa forma : String url = “jdbc:mysql://10.38.128.21/localhost:3306/db_controleci”;
informa que minha maquina não tem permissão ao BD.
java.sql.SQLException: null, message from server: “Host ‘micro005’ is not allowed to connect to this MariaDB server”
M
MoisesJava
Abner, o .JAR na minha maquina está conectando, dei um reset no privilégios do mysql e conectou normalmente:
Mas no outro pc dá erro de conexão. Tenho que realizar algum procedimento de liberação?
F
FearX
Tem que ver as regras do firewall.
M
MoisesJava
Funcionou na rede, mas ao construir o projeto ele cria uma pasta lib com os drivers. Para funcionar precisei levar o .JAR juntamente com a pasta lib. Teria alguma maneira que eu possa fazer para não ter que levar a pasta lib?
obrigado
R
rodriguesabner1 like
Sim, vai em “Files”, seleciona o seu projeto, e procura o arquivo chamado build.xml. E aí cola isso depois dos comentários:
<targetname="-post-jar"><propertyname="store.jar.name"value="${application.title}"/><propertyname="store.dir"value="store"/><propertyname="store.jar"value="${store.dir}/${store.jar.name}.jar"/><echomessage="Packaging ${store.jar.name} into a single JAR at ${store.jar}"/><deletedir="${store.dir}"/><mkdirdir="${store.dir}"/><jardestfile="${store.dir}/temp_final.jar"filesetmanifest="skip"><zipgroupfilesetdir="dist"includes="*.jar"/><zipgroupfilesetdir="dist/lib"includes="*.jar"/><manifest><attributename="Main-Class"value="${main.class}"/></manifest></jar><zipdestfile="${store.jar}"><zipfilesetsrc="${store.dir}/temp_final.jar"excludes="META-INF/.SF, META-INF/.DSA, META-INF/*.RSA"/></zip><deletefile="${store.dir}/temp_final.jar"/></target></project>
M
MoisesJava
Abner Bom Dia, fiz o procedimento , mas ele não acrescentou a pasta lib no .JAR. apenas o .JAR nos outros micros não faz a conexão com o banco.
R
rodriguesabner1 like
Opa! Boa tarde, tem certeza que fez o procedimento correto? Abriu o arquivo da pasta store? Porquê eu utilizo esse meio sempre e os arquivos sempre acoplam tudo no .jar.
Tenta abrir o .jar pelo CMD e vê qual erro apresenta pra você, aí você retorna
M
MoisesJava
Bom Dia, Fiz conforme você descreveu, segue:
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
<targetname="-post-jar"><propertyname="store.jar.name"value="${application.title}"/><propertyname="store.dir"value="store"/><propertyname="store.jar"value="${store.dir}/${store.jar.name}.jar"/><echomessage="Packaging ${store.jar.name} into a single JAR at ${store.jar}"/><deletedir="${store.dir}"/><mkdirdir="${store.dir}"/><jardestfile="${store.dir}/temp_final.jar"filesetmanifest="skip"><zipgroupfilesetdir="dist"includes="*.jar"/><zipgroupfilesetdir="dist/lib"includes="*.jar"/><manifest><attributename="Main-Class"value="${main.class}"/></manifest></jar><zipdestfile="${store.jar}"><zipfilesetsrc="${store.dir}/temp_final.jar"excludes="META-INF/.SF, META-INF/.DSA, META-INF/*.RSA"/></zip><deletefile="${store.dir}/temp_final.jar"/>
-->
Quando executo pelo CMD ele não consegue localizar a classe principal, mesmo com a pasta lib junto com o .JAR
R
rodriguesabner
Cara, manda o seu xml, eu acho que vc colocou errado.
M
MoisesJava
Meu xml é esse.
<?xml version="1.0" encoding="UTF-8"?>
Builds, tests, and runs the project ProjetoCI.
<!--
Thereexistseveraltargetswhicharebydefaultemptyandwhichcanbe
usedforexecutionofyourtasks.Thesetargetsareusuallyexecuted
beforeandaftersomemaintargets.Theyare:-pre-init:calledbeforeinitializationofprojectproperties
-post-init:calledafterinitializationofprojectproperties
-pre-compile:calledbeforejavaccompilation
-post-compile:calledafterjavaccompilation
-pre-compile-single:calledbeforejavaccompilationofsinglefile
-post-compile-single:calledafterjavaccompilationofsinglefile
-pre-compile-test:calledbeforejavaccompilationofJUnittests
-post-compile-test:calledafterjavaccompilationofJUnittests
-pre-compile-test-single:calledbeforejavaccompilationofsingleJUnittest
-post-compile-test-single:calledafterjavaccompilationofsingleJUunittest
-pre-jar:calledbeforeJARbuilding
-post-jar:calledafterJARbuilding
-post-clean:calledaftercleaningbuildproducts
(Targetsbeginningwith'-'arenotintendedtobecalledontheirown.)
Exampleofinsertinganobfuscatoraftercompilationcouldlooklikethis:
<targetname="-post-compile"><obfuscate><filesetdir="${build.classes.dir}"/></obfuscate></target>
Forlistofavailablepropertieschecktheimported
nbproject/build-impl.xmlfile.
Anotherwaytocustomizethebuildisbyoverridingexistingmaintargets.
Thetargetsofinterestare:-init-macrodef-javac:definesmacroforjavaccompilation
-init-macrodef-junit:definesmacroforjunitexecution
-init-macrodef-debug:definesmacroforclassdebugging
-init-macrodef-java:definesmacroforclassexecution
-do-jar:JARbuilding
run:executionofproject-javadoc-build:Javadocgeneration
test-report:JUnitreportgeneration
Anexampleofoverridingthetargetforprojectexecutioncouldlooklikethis:
<targetname="run"depends="ProjetoCI-impl.jar"><execdir="bin"executable="launcher.exe"><argfile="${dist.jar}"/></exec></target>
Noticethattheoverriddentargetdependsonthejartargetandnotonlyon
thecompiletargetastheregularruntargetdoes.Again,foralistofavailable
propertieswhichyoucanuse,checkthetargetyouareoverridinginthe
nbproject/build-impl.xmlfile.<targetname="-post-jar"><propertyname="store.jar.name"value="${application.title}"/><propertyname="store.dir"value="store"/><propertyname="store.jar"value="${store.dir}/${store.jar.name}.jar"/><echomessage="Packaging ${store.jar.name} into a single JAR at ${store.jar}"/><deletedir="${store.dir}"/><mkdirdir="${store.dir}"/><jardestfile="${store.dir}/temp_final.jar"filesetmanifest="skip"><zipgroupfilesetdir="dist"includes="*.jar"/><zipgroupfilesetdir="dist/lib"includes="*.jar"/><manifest><attributename="Main-Class"value="${main.class}"/></manifest></jar><zipdestfile="${store.jar}"><zipfilesetsrc="${store.dir}/temp_final.jar"excludes="META-INF/.SF, META-INF/.DSA, META-INF/*.RSA"/></zip><deletefile="${store.dir}/temp_final.jar"/>