-1

i have a problem with file cration in java. i found this question Java IO: file is not generated

but that did not provide an answer.

My tomcat webapp can create a file xml on remote folder(\myserver\myfolder) when is running on my dev enviroment. when i publish it on the test and rpod enviroment, my app does not create anything at all and those two machines are in the same network where my local enviroment is and they have also the same grants. It also does not throws any exception!!!

here's my code:

try{            
        Util.useNet(true, "\\\\myserver\\myfolder\\");
        fos = new FileOutputStream("\\\\myserver\\myfolder\\")+ fileName);
        fos.write("<?xml version=\"1.0\"?>".getBytes("UTF-8"));
        byte[] bytes = xml.getBytes("UTF-8");
        fos.write(bytes);
        fos.flush();
    }catch (Exception e){
        Log.setErrorLog("errore", e);
    }
    finally{
            try{
                fos.close();
            }catch (Exception e) {

            }
    }

the method "useNet" also call the command "net use" (just to be sure).

BohBah
  • 109
  • 1
  • 3
  • 11
  • 2
    That code won't even compile - `"\\myserver\myfolder\"` isn't a valid string literal. – Jon Skeet Jan 27 '15 at 10:50
  • my mistake in writing here! infact it is written so in my very program: "\\\\myserver\\myfile\\". and if you want to know, in my program i get it from a configuration file! if you have something constructive, please share – BohBah Jan 27 '15 at 10:52
  • checked the folder permissions?? – Prashant Jan 27 '15 at 10:56
  • You might want to try to use FileSystem.getSeparator() instead of actually typing "\\". If it works on your dev system and not on your other environments, my guess is that it's because of different platforms having different separators. – Pieter De Bie Jan 27 '15 at 10:58
  • And what is `Util.useNet`? A short but complete program demonstrating the problem would help. – Jon Skeet Jan 27 '15 at 10:58
  • Don't use backslashes in Java filenames. Forward slashes work just fine. – user207421 Jan 27 '15 at 11:02
  • use net execute this: command = "cmd /c net use \\\\myserver\\myfile mypassword /user: myuser – BohBah Jan 27 '15 at 11:06
  • Can you add logging lines to see this code is being called at all? – Peter Lawrey Jan 27 '15 at 11:09

1 Answers1

0

thank you all for your answer, i found them very helpfull! :)

i found the problem: it was in the moment we call the method "net use".

first of all "net use" does not accept a path with slashes at the end. Example:

 cmd net use /c \\\\myserver\\myfolder\\ mypassword /user:mydomain\\myuser

this command nomally throws an exception if it is write correctly but in my case thatit doesn't because of this:

/user:mydomain\\myuser

i noticed thah when i was getting from my file.properties the property "mydomain\myuser", its value was this: "mydomain\myuser".

java does not like single slashes, so the result was a "net use" command that was like this:

cmd net use /c net use \\\\myserver\\myfolder\\ mypassword /user:mydomain@myuser

this was the cause of the non-throwing-exception problem. Fixed that, it throws "filenotfoundexception: access denied" but this is another story.

So, at the end, here is the "net use" command correct:

 cmd net use /c \\\\myserver\\myfolder mypassword /user:mydomain\\myuser

A stupid distraction error i feel ashame, sorry everyone!

Thank you all again for your help

BohBah
  • 109
  • 1
  • 3
  • 11