write java file to C drive access error - java

Hi how can I get rid of C drive access with java codes I can do this with manual but I want to do this with java codes
here is my code
File file=new File("C:\\Windows\\b.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("file not created and");
e.getMessage();
e.printStackTrace();
}
And my exceptions
java.io.IOException: Access is denied
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1006)
at tekrar.Write.main(Write.java:26)`

you need to run the program as an administrator to edit the C:\Windows folder. this is a system folder that you should not access.

You cannot write anything in the system folder without administrator privileges in windows. As I understand you cannot request administrator access from within a Java program and you'll have to RUN the application in admin mode.
See this question for some tips: Run Java file as Administrator with full privileges
P.S. Do you really need to write into the windows folder?

Related

Desktop.setOpenFileHandler doesn't work when program isn't running

I'm making a program for mac that has it's own file extension .edul. I got it working so that when the program has already opened and you open the file it's load it, but when I try this when the program is yet running I don't get the event and it's not in the args.
I tried to change to com.apple.eawt.Application but unfortunately java.desktop doesn't export this class so this didn't work.
Desktop desktop = Desktop.getDesktop();
desktop.setOpenFileHandler(e -> {
try {
FileWriter fileWriter = new FileWriter(System.getProperty("user.home") + "/Desktop/test.txt");
fileWriter.write(e.getFiles().toString());
fileWriter.close();
} catch (IOException ignored) {
}
});
So I expect a file on my desktop that has the location of the file I opened before the program was running and when it's running and I open another file it's also shows on my desktop.
So after first file open:
[file/path/test.edul]
And on seconds file open:
[file/path/test2.edul]
I'm currently only getting:
[file/path/test2.edul]
So, the problem is that when you define a FileOpenHandler in the class which starts your fx application the FileOpenEvent is only thrown when the program is already running. So you should make a other main class that doesn't extends Application and the FileOpenEvent will be correctly thrown even when the application is still starting up.

How to open TestComplete from java code

I would like to open TestComplete from java, but I can't do that, because lack of privilege. When I run my code
public static void StartTC() {
try{
Process p = Runtime.getRuntime().exec(new String[] {"C:\\Program Files (x86)\\SmartBear\\TestComplete 11\\Bin\\TestComplete.exe"});
}
catch (IOException e) {
e.printStackTrace();
}
}
the program exits with CreateProcess error=740, and tells me that I need higher privilege for this action.
I know that I could make a .lnk with admin priv. at open properties of the exe, but there could be a right way to do this.
I think you can use File class for setting permissions.
File file = new File("File.c");
//but file permission are OS specific.
file.setExecutable(true);
In linux it will work.
If you are using windows then you can run "icacls" command to give permission to the file.
C:\>icacls "D:\test" /grant John:(OI)(CI)F /T
This command can be used to to give permission in windows.
According do MS documentation:
F = Full Control
CI = Container Inherit - This flag indicates that subordinate containers will inherit this ACE.
OI = Object Inherit - This flag indicates that subordinate files will inherit the ACE.
/T = Apply recursively to existing files and sub-folders. (OI and CI only apply to new files and sub-folders). Credit: comment by #AlexSpence.
You can run above command using Runtime.getRuntime().exec("icacls something here");
I hope I helped you.
You need to disable the Tools | Options... | Engines | General | Enable support for testing Windows Store applications option in TestComplete.
Information on how this can affect working with TestComplete from an external application like in your case can be found in the Requirements for Testing Windows Store Applications help topic.

Opened file in Java and in Windows file cannot be accesed, even Java is not using file anymore

I have directory where I will look for ZIP files and then I will extract files from those ZIP files with TrueZIP 7. My problem is that when I have my application running, I cannot delete/edit file while application is running in Windows. I don't understand why this is happening, because when I'm accessing file in Windows my application is not doing anything with files, so why these files are locked to my Java app?
I have following code:
Path dir = FileSystems.getDefault().getPath(directory);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path file : stream) {
// do something with file
}
} catch (IOException | DirectoryIteratorException x) {
System.err.println(x);
}
// Why those files are still locked to my app even when execution is here ???
I forgot to call unmount :
TVFS.umount();
Now I can delete/edit files.

Connecting to Quality Center v11 using COM4J

I am trying to connect to HP Quality Center V11 using Java code and com4j but i keep getting following error. Can someone please take a look at the error?
When I use the URL in my browser and log-in with same credentials, I was able to login. I double checked all the spelling of my domain, url, Id and password..
Error I get:
com4j.ComException: 800403ea (Unknown error) : Failed to Login : .\invoke.cpp:517
at com4j.Wrapper.invoke(Wrapper.java:166)
at $Proxy5.connectProjectEx(Unknown Source)
at com.testpack.TestClass.main(TestClass.java:23)
Caused by: com4j.ComException: 800403ea (Unknown error) : Failed to Login : .\invoke.cpp:517
at com4j.Native.invoke(Native Method)
at com4j.StandardComMethod.invoke(StandardComMethod.java:35)
at com4j.Wrapper$InvocationThunk.call(Wrapper.java:340)
at com4j.Task.invoke(Task.java:51)
at com4j.ComThread.run0(ComThread.java:153)
at com4j.ComThread.run(ComThread.java:134)
Code I use to connect
public static void main(String[] args) {
String url="http://XXXX/qcbin/";
String domain="ACTIVE";
String project="QC_2013_Projects";
String username="XXXX";
String password="XXXXX";
try{
ITDConnection itd=ClassFactory.createTDConnection();
itd.initConnectionEx(url);
System.out.println("Test1:"+ itd.connected());
itd.connectProjectEx(domain,project,username,password);
//System.out.println(itd.connected());
}catch(Exception e){
e.printStackTrace();
}
}
I followed these steps for getting connected to HP QC 11 from Java code using com4j on windows 7 32 bit machine
Download Com4j artefacts com4j-20120426-2.zip from https://github.com/downloads/kohsuke/com4j/com4j-20120426-2.zip
Unzip it. Open a command prompt and navigate to the unzipped folder. Then run following command to create Wrapper classes in a location CCCC with package structure as DDDD.
java -jar tlbimp.jar -o "C:\CCCC" -p "DDDD" "C:\Users\MYACC\AppData\Local\HP\ALM-Client\10\OTAClient.dll"
Now copy the OTAClient.dll and WebClient.dll from C:\Users\MYACC\AppData\Local\HP\ALM-Client\10 and save it in Windows/System32 folder.
After following the step 2, you must have a com4j-x86.dll in the location where tlbimp.jar is present. Now copy that dll to Windows/System32 folder.
Now with Admin rights, register all 3 dll files using the commands 1 by 1 as follows.
regsvr32 com4j-x86.dll
regsvr32 OTAClient.dll
regsvr32 WebClient.dll
Now create a Java Project in eclipse. In the src folder copy the DDDD folder created during the step 2. Add com4j.jar in class build path. Then have the following code in a java file to test the HP QC connection. Run the java file to check the result.
ITDConnection itd=ClassFactory.createTDConnection();
itd.initConnectionEx("http://10.10.10.10:8080/qcbin");
System.out.println(itd.connected());
itd.connectProjectEx("DOMAIN_NAME", "PROJECT_NAME", "HPQC_USERID", "HPQC_CREDENTIAL");
System.out.println(itd.projectConnected());
Hope this helps. :)
I was finally able to solve this issue. I had install HP ALM QC Client. It would install in the following path- > Your Program files->HP->HP ALM Client.
After I installed this, I was able to connect to QC.
Hope this might be useful to someone else. Thanks!
I added all the 3 .dll files in c:\Windows\SysWOW64 and executed the same code.
// This is a fairly important section for x64 bit machines,
// as a note this took me forever to figure out. Basically,
// if the DLL is not registered in the SysWOW64 dir then we
// are unable to use this as it was created when 32-bit
// computers were still all the rage. This is a quick little
// hack that registers it if it is needed to be registered.
// If it's already registered, this does nothing.
try {
Runtime.getRuntime()
.exec("C:\\windows\\SysWOW64\\regsvr32 /s lib\\OTAClient.dll")
.waitFor();
Runtime.getRuntime()
.exec("C:\\windows\\SysWOW64\\regsvr32 /s lib\\com4j-amd64.dll")
.waitFor();
Runtime.getRuntime()
.exec("C:\\windows\\SysWOW64\\regsvr32 /s lib\\com4j-x86.dll")
.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Utilities
.showError(
new JFrame(),
"OTAClient.dll, com4j-amd64.dll or com4j-x86.dll could not "
+ "be registered, program may or may not work on a 64-bit machine "
+ "without these files. You can attempt to manually register them, "
+ "but this rarely works.");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run this before anything else in your code, it's what I used to force any machine to register them.

java.io.IOException: Permission denied on network folder

i'm having the the post's title error when trying to write a file on a window folder , mounted on unix system. I've developed a web service which runs inside a Tomcat 6 on a linux os and need to write on a windows network folder. System administrators have mounted it on the Linux sever and have no problem to create and modify a file on it.
When i try to execute the posted code i get the following exception :
Permission denied
java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:850)
The weird thing is that it seems to be related to the File.createNewFile method on a network folder , in fact the service can write on local file system without problems, both on debug (the pc i use to develop the service) and a tomcat folder system administrators have provided me on the linux server. The file gets created but is empty and the log entry following the create method doesn't get printed. Moreover if i use a plain outputstream to create and write the file i've no problems.
I cannot find any explanation about the exception on the web. Since i'm not very experienced with java , i'd like to understand why i'm getting this error. Am i using it in the wrong way ? Is it a bug of the library ? Do i miss to pass some parameter ?
As stated , i've solved the problem using a plain outputstream, this is a question to improve my understanding of java.
FileOutputStream fos = null;
try{
log.info(String.format("file length: %s",streamAttach.length));
log.info(String.format("check File : %s",filename));
File f = new File(filename);
if(f.exists())
...
boolean done= f.createNewFile();//here comes the exception
//nothing of the following happens
if(!done)
throw new NWSException("error creating file");
log.info(String.format("file %s creato", nomeFile));
thank you in advance for any answer
I ran into this problem recently and found that java.io.File.createNewFile() actually requires the "Change Permissions" permission (you can find this entry under Security->Advanced when checking folder permissions). Without this it will create the file and then subsequently throw an IOException.
It's deceptive because you will still be able to create files on the folder when manually testing, however createNewFile() will still fail if it doesn't have this particular permission (presumably such that it can change the permissions on the file its creating).
If you are using Netapp that shares an NTFS (CIFS) style filesystem to Unix you could be experience "NFS is not allowed to change permissions on a file in an NTFS-style security volume." (TR-3490 page 16)
Options here are to change to a unix filesystem or set the cifs.ntfs_ignore_unix_security_ops flag to on for the file system which quiches the NFS permission error.
java.io.UnixFileSystem.createFileExclusively(Native Method) opens the file with the O_EXCL and 0666 umask so I would get a EACCES, which really was a NFS3RR_ACCES
open("/net/storage01-a/filer/myfile", O_RDWR|O_CREAT|O_EXCL, 0666) Err#13 EACCES
Also you can use OutputStream to create the file, that does not use O_EXCL it seemes
It definitely not Java specific problem. If this Unix folder is mapped to your windows try to open file explorer and create file in this directory. I believe that you will get permission denied too. In this case fix this problem or ask your system administrator to help you.
Good luck!

Categories

Resources