Java ImageIO.read causing OSX to hang - java

I'm having to do some bits with reading in an image on Mac OSX, however it seems to hang when calling ImageIO.read ( File ). No stack trace seems to appear either, it literally just hangs. Was wondering if anyone else had experienced this problem?
I've been successful in writing an image, just seems to be a problem with reading. Working with .png files.
OSX 10.9.2
Java 1.7.40

The solution I found in the end was to do the following on the mac
File scrFile = ((TakesScreenshot)seleniumCommonHandler.getCurrentSeleniumDriver())
.getScreenshotAs(OutputType.FILE);
BufferedImage img = ImageIO.read( scrFile );
For some reason the ImageIO no matter what I tried could not read the image directly from the file system. By using this approach I managed to get around the issue.

Related

Get File Version of .exe in java on Linux

Question - Get File Version of .exe in java on Linux for some strange client.
Solution -
I used JNA library to read file version using Java. Given below code is running fine on windows platform but it is throwing below error on Linux docker image.
"Unable to load library 'version': Error loading shared library libversion.so: No such file or directory Error loading shared library libversion.so: No such file or directory Native library (linux-x86-64/libversion.so) not found in resource path..".
private String GetFileVersion(String filePath) {
File fileToCheck = new File(filePath);
short[] rtnData = new short[4];
int infoSize = Version.INSTANCE.GetFileVersionInfoSize(fileToCheck.getAbsolutePath(), null);
Pointer buffer = Kernel32.INSTANCE.LocalAlloc(WinBase.LMEM_ZEROINIT, infoSize);
try {
Version.INSTANCE.GetFileVersionInfo(fileToCheck.getAbsolutePath(), 0, infoSize, buffer);
IntByReference outputSize = new IntByReference();
PointerByReference pointer = new PointerByReference();
Version.INSTANCE.VerQueryValue(buffer, "\\", pointer, outputSize);
VerRsrc.VS_FIXEDFILEINFO fileInfoStructure = new VerRsrc.VS_FIXEDFILEINFO(pointer.getValue());
rtnData[0] = (short) (fileInfoStructure.dwFileVersionMS.longValue() >> 16);
rtnData[1] = (short) (fileInfoStructure.dwFileVersionMS.longValue() & 0xffff);
rtnData[2] = (short) (fileInfoStructure.dwFileVersionLS.longValue() >> 16);
rtnData[3] = (short) (fileInfoStructure.dwFileVersionLS.longValue() & 0xffff);
return String.format("%s.%s.%s.%s", rtnData[0], rtnData[1], rtnData[2], rtnData[3]);
} catch (Exception exception) {
return null;
} finally {
Kernel32.INSTANCE.GlobalFree(buffer);
}
}
I will start by answering the question that you asked, though I doubt it is what you actually need to know.
The types of different executable file formats are encoded in the first few bytes of the file. For example, ELF files (executables, shared libraries) are described in this Wikipedia page.
So there are a number of ways to find out what kind of executable in Java:
Write some code that reads the first few bytes and decodes the file header information, as per the format described in the Wikipedia link above.
Find an existing Java library that does this and work out how to do this. (Google for "java file magic library" and see what you can find.)
Read about the Linux file command and write some Java code to run file on each library and parse the output.
What I think you actually need to do is a bit different:
Locate the file or files in the file system that the Java is looking for: apparently libversion.so or linux-x86-64/libversion.so. (The file could well be a symlink. Follow it.)
Run file on each file to check that it is the right kind of library. They need to be 32 or 64 bit corresponding the JVM you are running, and the correct ABI and ISA for the platform.
Check that the files are where the JVM expects to find them. The JVM searches for libraries in directories listed in the "java.library.path" system property. You can (if necessary) set the path using a -Djava.library.path=... JVM option.
See "java.library.path – What is it and how to use" for more information on library loading.
(There is absolutely no need to do step 2 "from" or "in" Java.)
I think I have finally worked out what you are doing.
The Version you are using is actually coming from the package com.sun.jna.platform.win32. It is not part of the JNA library (jna.jar). I think it is actually part of jna-platform.jar. If I understand things correctly, that is the generated JNA adapter library for the Windows COM dlls.
If I have that correct, you would actually need the Windows COM native libraries compiled and built for the Linux platform to do what you are trying to do.
AFAIK, that's not possible.
So how could you make this work? Basically you need to do one of the following:
Look for an existing pure Java library for extracting the version information from a Windows ".exe" file. I don't think it is likely that you will find one.
Find the specification for the Windows ".exe" file format and write your own Java code to extract the version information. I haven't looked for the spec to see how much work it would be.
Then you rewrite the code that you added your question to use the alternative API.
The "libversion" file that I mentioned in my other answer is not relevant. It is something else. It is a red herring.

OpenCV on Windows 10 can't load video file

I'm trying to learn OpenCV 3.2.0 with Java and I am having trouble loading in a video file into the program. I work on Eclipse in Windows 10. This is a snippet from the code:
Mat frame = new Mat();
Mat frameHSV = new Mat();
VideoCapture vcap = new VideoCapture("C:\\Users\\UserName\\Downloads\\video.mov");
while(vcap.read(frame))
{
frameHSV = ColorBase.BGRToHSV(frame);
displayWindow.updateFrame(frameHSV);
}
When I launch this program, I only get a white window with nothing in it. I think this has something to do with this being on windows, as I tried it out on a Linux computer and it worked as intended. This means the path points to the correct location and that the code actually works.
Is this an issue with OpenCV and Paths on windows? I get no complile errors, only a white window. How is this fixed?
I managed to solve it by following another thread on this site that I didn't find before:
OpenCV Java binds VideoCapture from file failing silently
I followed the second answer on this thread and it worked perfectly after that.
Its either a video codec, or permission issue, can you move the file to other drive like: D:\\video.mov and try?

Using nsis to package application installer and .wav file does not play because of a path on Windows containing a space --> Program%20Files

I'm running into an issue that involves an audioInputStream, a resource folder, and nsis for a Windows installation. I'm working on an app (in Linux) that performs a desktop notification when an event occurs and everything works except for the .wav file that is supposed to play when the notification pops up. I have tested the app on a 64 bit Windows machine without installing it via nsis and it works perfectly. I received an error message indicating:
07/08/13 12:17:26 ERROR [Thread-2] (DesktopNotifierMessageAlertHandler.java:73) com.alcatel.proserv.e911.desktopNotifierMessaging.desktopNotifierMessageHandler.DesktopNotifierMessageAlertHandler - Error: java.io.FileNotFoundException: C:\Program%20Files\Alcatel-Lucent\E911DesktopNotifier\classes\audio\siren.wav (Le chemin d'accès spécifié est introuvable)
I'm working in Netbeans and using maven to build. Here is a code snippet of how I'm loading the path:
String filename = this.getClass().getResource("/audio/siren.wav").getPath();
AudioInputStream audioInputStream = null;
try{
audioInputStream = AudioSystem.getAudioInputStream(new File(filename).getAbsoluteFile());
Clip clip = null;
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
}
...
I found this blog detailing how to fix an extremely similar issue:
http://braintwitter.blogspot.ro/2013/03/url-encoding-issue-with-tomcat.html
but it didn't work out with the audioInputStream I'm working with.
I know it's a problem with the space in "Program Files" which is where I have to set up the installation to occur because when I changed the InstallDir value in the setup.nsi script from $PROGRAMFILES64 to $WINDIR, it worked perfectly.
Does anyone have any suggestions for how I can modify my code to work properly since the space in Program Files is causing an encoding issue?
getResource() returns a URL, and it has URL encoding applied here. You have two options. You can convert to a URI:
String filename = this.getClass().getResource("/audio/siren.wav").toURI().getPath();
Or you can use URLDecoder to decode the path before passing it along to your AudioInputStream:
String filename = this.getClass().getResource("/audio/siren.wav").getPath();
filename = URLDecoder.decode(filename, "utf-8");
See the blurb at the end of the intro section for java.net.URL.
so I know this is pretty old thread, but anyone reading this, think twice, as the nsis packager creates this malware as scanned on virus total on a pc that's been run with malwarebytes, avira, roguekiller, hitman pro and stinger.
Antiy-AVL - Trojan/Generic.ASMalwS.3506C16, Avast-Win64:Malware-gen, AVG - Win64:Malware-gen, Cyren - W64/Tedy.B.gen!Eldorado, Jiangmin -Trojan.PSW.Python.fv, Zillya -Trojan.Disco.Script.653

Reading File in J2ME

I became so much upset with this simple code segment:
FileConnection fc = (FileConnection) Connector.open("file:///root1/photos/2.png");
System.out.println(is.available());
byte[] fileBytes = new byte[is.available()];
int sizef = is.read(fileBytes);
System.out.println("filesize:"+sizef);
When I deploy this midlet in my mobile (with proper file location) It works properly i.e it shows proper filesize, but in the pc emulator it is constantly giving filesize of: 0. Obviously no Exception and I have the proper file in that location.
I am using j2mewtk sdk in netbeans 6.9. I tried uninstalling, installing wtk & netbeans.
Another thing is Everytime I run the emulator it creates C:\Users\Mahsruf\j2mewtk\2.5.2\appdb\temp.DefaultColorPhone6 new location like temp.DefaultColorPhone1,2,3,4 etc.
If I use jme SDK 3.0 in netbeans the file size is still 0, but now with a extra line in output window: [WARN] [rms ] javacall_file_open: _wopen failed for: C:\Users\Mahsruf\javame-sdk\3.0\work\0\appdb\_delete_notify.dat
What am I doing wrong?
This is not coding related issue. If multiple instances of the same emulator skin run simultaneously, the toolkit generates unique file paths for each one. For example, on Windows instances of DefaultColorPhone might have a file path name of workdir\appdb\temp.DefaultColorPhone1, workdir\appdb\temp.DefaultColorPhone2, and so forth.
Solution: The file workdir\appdb\DefaultColorPhone\in.use keeps track of the number of storage roots marked as in use. If the emulator crashes, you need to delete the in.use file

Open Image with the Windows Picture and Fax Viewer (Java)

I have a picture which I would like to open with thewindows Picutre and Fax Viewer. How do you do that? I was able to open it with mspaint of which I know the exe File. The Code is the following:
File imageFile = new File("filepath" + System.currentTimeMillis()+".png");
ImageIO.write(printImg, "PNG", imageFile);
String application = "mspaint.exe";
Runtime.getRuntime().exec(application + " \"" + imageFile.getAbsolutePath()+"\"");
Does anybody know the exe of the Windows Picture and Fax Viewer?
I was looking for a solution to this and managed to adapt one I found on a C# forum.
Windows Picture and Fax is actually a .dll, not a .exe, which is why it isn't immediately visible. Using the line...
Runtime.getRuntime().exec("rundll32.exe E:\\WINDOWS\\System32\\shimgvw.dll,ImageView_Fullscreen "+filename);
worked just ducky for me. fileName is type String, in case it isn't obvious. Also, Windows is on drive E, not C, for me. Escape characters and all those other fun little roadbumps.
Hope this helps, and sorry I didn't get to this problem earlier!
I'm not sure but when I open up task manager and select "Go to Process" on the Windows Picture and Fax Viewer..it says that it runs under the explorer.exe process.

Categories