I have an app that runs fine when I am not attempting to use it as a web start app, but when I convert it to one in Netbeans, it goes belly up. The console is showing a null pointer exception when trying to get a resource that is located in a dependent library. The line of code itself grabs a .png file in that library file and uses it for the window icon. The lines of code that kill it is:
java.net.URL url = ClassLoader.getSystemResource("/com/my/icon/someimage.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url); // fails with null pointer exception
If I am not trying to do this as a web start app then I have zero issues. It runs perfectly. Web start = failboat. The code is signed, if that makes any difference. The library file that is being used is one large jar that contains 3 library files I created plus 4 others that are needed which I did not code myself. Do I need to sign the library file as well? Is this possibly an issue in and of itself?
EDIT:
If I take out those lines of code, the web app runs without any issues.
Here is the error:
Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
at sun.net.util.URLUtil.getConnectPermission(Unknown Source)
at sun.awt.SunToolkit.checkPermissions(Unknown Source)
at sun.awt.SunToolkit.createImage(Unknown Source)
at DDSC.initComponents(DDSC.java:265)
at DDSC.<init>(DDSC.java:103)
Try using getClassLoader().getResource() with just the filename.
Full path isn't required and getClassLoader() handles the magic via WebStart & also locally:
String filename = "someimage.png";
Image resultImg = null;
URL url = getClassLoader().getResource(filename);
if (url != null)
resultImg = Toolkit.getDefaultToolkit().getImage(url);
Related
I have integrated Image magick with a Java WebApp and have deployed it on Azure App Service. On azure , I am getting 0kb as output image for a image while the same image gets converted fine on my local machine.
I am using im4java for integration with Image Magick.
Below is the code:
public void compressImage(String imageSource, String destinationPath) throws IOException, InterruptedException,
IM4JavaException {
ConvertCmd cmd = getCommand();
// create the operation, add images and operators/options
IMOperation op = new IMOperation();
op.strip();
op.interlace();
op.addRawArgs(compressionRawArguments);
op.gaussianBlur(compressionGaussianBlur);
op.quality(compressedImageQuality);
op.addImage(imageSource); // source file
op.addImage(destinationPath); // destination file
// execute the operation
cmd.run(op);
}
Both imageSource and destination are temp files created using java. I have checked that imageSource file has correct size but after running this code, the destination file is always 0 Kb.
Please advise what could I be doing wrong?
Answering my own question so that It might be helpful for fellow developers who might face this problem.
Azure App Service normally has Windows Server VMs. You can check the OS of your server in web container logs.
Image Magick for windows does not allow conversion of remote http
image urls while for Unix System, it allows so. My Local machine is
MAC So it was working correctly on my local system.
2 Solutions to this problem that I found:
Either you use a linux VM on Azure
In your application, download the image URL to a temp file and
supply the absolute path of that temp file to image magick for
conversion.
I have tried both and are both working.
How do I change the default application icon in Java?
I tried the above code using Netbeans 7.0 but I'm not sure where the resources directory needs to be? If I use the one Netbeans has under files listing. It keeps giving me this error. Also, is the code correct?
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:115)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:125)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)
using this code:
URL url;
url = ClassLoader.getSystemResource("/com/pbuddie/resources/Server.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
clientUI.setIconImage(img);
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
i am getting a problem
i have deployed a war file, when i run localy through tomcat it works fine but when i run on another system by giveing my system ip and then project folder e.g
http:\192.168.0.145\DllTest it loads the applet but when i click on a button to load the functionality it is throwing an exception
Exception in thread "AWT-EventQueue-3" java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: http:\192.168.0.145:8080\DllTest\lib\jinvoke.dll
while it is working fine localy but not in another system. Please tell me what is the problem.
Is it a rights issue or something else.
You cannot load a DLL on an external host. It has to be an absolute disk file system -as the exception message already hints. Your best bet is to download it manually, create a temp file and load it instead.
File dllFile = File.createTempFile("jinvoke", ".dll");
InputStream input = new URL(getCodeBase(), "lib/jinvoke.dll").openStream();
OuptutStream output = new FileOutputStream(dllFile);
// Write input to output and close streams the usual Java IO way.
// Then load it using absolute disk file system path.
System.loadLibrary(dllFile.getAbsolutePath());
dllFile.deleteOnExit();
I have the following line in my Java app, and it's causing the following error message :
WebBrowser webBrowser=new WebBrowser();
org.jdesktop.jdic.init.JdicInitException: java.io.IOException: The filename, directory name, or volume label syntax is incorrect
at org.jdesktop.jdic.init.JdicManager.initBrowserNative(Unknown Source)
at org.jdesktop.jdic.browser.WebBrowser.<clinit>(Unknown Source)
The app still works, but I wonder if there is a way to correct the error ?
There's a problem initializing jdic's native browser but it isn't obvious what it is.
Does passing a URL to the constructor help?
URL url = new URL("http://www.stackoverflow.com");
WebBrowser webBrowser = new WebBrowser();
One more thing to check - do you have a default browser? If so, when you double click on an html file in a file manager does the browser window open? If this doesn't work then perhaps jdic is complaining about your default browser path.