Android, Interesting

ClassNotFoundException when running Simple XML

The Problem

A few days ago I ran into a problem while using Simple XML in my Android codebase. Since Simple takes about 2-3 seconds to load my level files I could not perform that activity on the main UI thread; as per android design guidelines. So I threw the loading of my level into an AsyncTask as you are supposed to and thought that everything was just perfect. However, I quickly encountered a problem whereby all of my data would be written out correctly to an XML file but when I tried to read it back in I was given a ClassNotFoundException. The error looked something like this:

12-25 12:21:44.565: W/System.err(404): java.lang.ClassNotFoundException: MyCoolJavaObject in loader dalvik.system.PathClassLoader@4001b500
…more messages here…
12-25 12:21:44.575: W/System.err(404):  at org.simpleframework.xml.strategy.Loader.load(Loader.java:50)
…more messages here…
The Solution (Solving The Problem)

Whenever you push a polymorphic type into an XML file Simple XML also adds an attribute called ‘class’ and it is filled with the absolute package path of the object that was just serialised. Like this for example:

<fruit class="com.fruitseller.Apple" seeds="4">
<fruit class="com.fruitseller.Bananna"> 
    <parentTree id="4984" /> 
    <energyContent>98</energyContent> 
</fruit>

As you can see in this example, when Simple reads in these classes again it knows which constructors to use by looking up those classes in the hierarchy. Therefore, if that look-up process fails you are going to get a ClassNotFoundExeption, just like the one that I received above.

The ClassPathLoader is what is in charge of finding those classes and that is what needed to be fixed. As it turns out the new thread that the AsyncTask was running on did not have the same class path loader as the one that the UI thread of my app was using. Therefore, all I needed to do was set the class path loader in that AsyncTask before I ran any simple xml read or write functions. To do so I grabbed the one from my Application class, like this:

// The fix is this two lines
ClassLoader thisClassLoader = MyApplication.class.getClassLoader();
Thread.currentThread().setContextClassLoader(thisClassLoader);

// This Persister code
Serializer serial = new Persister();
// ...more simple code...
As you can see the fix ended up being a meager two line fix that can probably be condensed into one line. And that solved my problem completely. I hope that this helps you and if it does not feel free to ask questions in the comments.
Advertisements
Interesting, Troubleshooting

Fixing the PhysX “Failed To Install” Error

Did your version of PhysX fail to install or have you tried to play a game and it errors out and closes due to an error that could be related to missing PhysX libraries? Maybe Wise Installer is claiming that you are missing some files (extensions .wsi / .msi)? Then you have come to the right place and I am going to give you some instructions that should hopefully solve your problem.

The Problem

Recently I had a moment of Nostalgia and bought Overlord II from the Steam store but to my dismay, upon startup it encountered an error and aborted the program. This was the error message that I encountered:

Microsoft Virtual C++ Runtime Library

Assertion failed!!

Program: C:Overlord IIOverlord2.exe
File: ..FxSDKSrcFxName.cpp
Line: 461

Expression FxFalse != removeResult

For information on how your programa can cause an assertion
failure, see the Visual C++ documentation on asserts

(Press Retry do debug the application – JIT must be enabled)

I had no idea what could be causing that problem so I looked it up online and discovered people that said it was due to a PhysX installation error; in the end it turns out that was the case but it took some clever steps to make sure that I properly fixed the problem. Here is the quick guide to solving the problem in a few steps as possible.

The Solution

This problem is usually caused by PhysX trying to get rid of the last installation before it upgrades and failing to do so because it was only a partial install. So to fix that first we have to reinstall/fix the version that was supposed to install properly and then move on to installing the version that you want to install. As one of the viewers of this page put it:

It’s a one step backwards, two steps forwards kinda situation.

Without any further explanation lets just jump straight into the steps:

Important: These steps apply to Windows 7 though they may work in Windows Vista.

Step 1: Discover what version of PhysX installed to your machine or what version your machine thinks it has installed.

To do this press the start button and type in “Programs and Features” and run it:

Open "Programs and Features"

Then once that is open wait for the list of installed items to populate (it may take some time or be instant depending on your computer) and then scroll down to “Nvidia PhysX”. The version number that is supposed to be installed will be present on the far right (click on the image to enlarge it):

Using Programs and Features to find the PhysX Version number.

So as you can see my currently installed version number is “9.11.1107” but yours will be different and that is the one you want because it will be important in the next section.

Step 2: Download the version of PhysX that should have installed correctly but did not.

Important: If you want an easy way to do this just use this simple little form I wrote: http://jsfiddle.net/robertmassaioli/k4y56/7/embedded/result/

This paragraph until Step 3 is now old and is only useful if the link breaks and you need to generate the url yourself. The correct way of doing this is to go to:

http://www.nvidia.com/object/physx-<version_number>-driver.html

Where you are supposed to replace <version_number> with the correct version that you discovered in the step above and then navigate to that url in your browser.

Note: If you did not manage to find the correct download (you get ‘File Not Found’ errors) then please google “physx <version_number>” to find the correct installer / executable for you to run in the next step.

Step 3: Fix the initial install of the program.

Run the program that you downloaded and you should get a screen that asks you to Modify, Repair or Remove the program. You want to select ‘Modify’ and click ‘Next’. If you do not get the screen shown below then you have downloaded the incorrect installer in step 2:

Once you have done that just keep selecting the next option and it should clean up your entire install and run successfully. If it does not then yet another problem has occurred and this guide may not be able to help you further. You may have some luck with an alternative solution that David Orders has suggested in the comments section.

Step 4: Install the latest version of PhysX

Now that you have fixed the installation of PhysX you are free to download and install the latest version of PhysX onto your machine. Go here to do so: http://www.nvidia.com/object/physx_system_software.html

Conclusion

That is all that there is to it. You should now have PhysX working correctly on your machine and be playing games and anything that you want in no time. If you continue to have problems then mention them in the comments to help other people that are trying to solve the same problem. I hope this helps.