"java.exe" exited with code 2 Xamarin.Android project - java

so I have my Xamarin.Android project which contains several libraries. When I try to build I receive an error which says "java.exe" exited with code 2.
After some hours of google research I found out that I could enable Multidex.
According to the blog entry of Jon Doublas I did the steps mentioned.
http://www.jon-douglas.com/2016/09/05/xamarin-android-multidex/
According to the blog entry Android API Level > 21 handles Multidex itself.
Well my experience is that it does not. I am building on API Level 25 and get the same error again and again with the same information.
The steps in the article do not help. It seems like Xamarin.Android does not support Multidex.
It seems like you have to handle Multidex by yourself but I haven't found any solution which is working. Dos anybody found a solution which works?
I am using Xamarin 4.6 and Visual studio 2017.

Found the answer after about 20 hours of searching in my project..
So here are the steps to resolve the problem "java.exe" exited with code 2
1) Change the MainDexClasses.bat as described in the link:
http://www.jon-douglas.com/2016/09/05/xamarin-android-multidex/
2) Make sure you have enabled Multidex Option in your Android Application! Have a look at the .csprj file if Multidex is set to true (in my case it wasn't even if I enabled Multidex)
3) Add the following part in your AndroidManifest file
<application android:name="android.support.multidex.MultiDexApplication"></application>
This will tell your Android project to actually use the Multidex-Option given.
After a clean and build I was able to run my application as it is.

After finding this question and #Daniel_97s answer I was able to solve this error only by adding multidex to the project without changing MainDexClasses.bat:
Check the option in Project Options:
Add MultiDexApplication to Android Manifest:
<application android:name="android.support.multidex.MultiDexApplication"></application>
Note I was getting this error code 2 on Visual Studio for Mac and on Windows the same app was building successfully. (why?)

The solution in my case was to disable the "Sign the .APK file using the following keystore details". Because the keystore location and/or password was incorrect, java.exe exited with code 2.

I only go to option > Build > Android Build and check Enable Multi-Dex it will resolve

My .csproj file had an entry for the KeyStore that was no longer valid, even thought right-clicking the project and inspecting the properties indicated that I had pointed it to the new location.
Within .csproj look for all instances of <AndroidSigningKeyStore> and update accordingly.

I've struggled with this issue for quite a long time. I found source of the error in signing - as I cloned project repo from company TFS, Android csproj file had entries for KeyStore settings and location. I didn't have the keystore and the location on my drive was invalid. Visual Studio is then unable to sign the app while deploying and instead of some sort of FileNotFoundException it gave me just java exited with code 2. So...
tl;dr: Go to android project properties, select package signing a uncheck "Sign the apk file..." (which changes csproj <AndroidKeyStore>true</AndroidKeyStore> to false) and voila, you can now run your solution.

Reassign the path of the keystore worked for me
==EDIT 01==
Double click on Android Project > Android Package Signing
On the Keystore path, don't know why but apparently the path was wrong, I re-select the .keystore file again from the same path, and the error "exited with code 2" went away and I was able to Archive for release

Disable sign the .APK file using the following keystore details as #Klemikaze

I had this error when switching from VS Mac to VS Windows.
Here the topic Switching From VS Mac To VS Windows got “java.exe” exited with code 2
The issue was comming from the Android.csproj and the Keystore Path.
It was set to my Mac Path even if I changed it on
VSWindows AndroidProject => Properties => Signin
So I removed this lines on Android.csproj (edited with third text editor):
<AndroidKeyStore>True</AndroidKeyStore>
<AndroidSigningKeyStore>YourMacPath/Alias.keystore</AndroidSigningKeyStore>
<AndroidSigningStorePass>Password</AndroidSigningStorePass>
<AndroidSigningKeyAlias>Alias</AndroidSigningKeyAlias>
<AndroidSigningKeyPass>AliasPassword</AndroidSigningKeyPass>
On debug & release Part.
Then you can edit them on VS Windows.
Hope I helped someone.
See ya

In my case this was link to the "Sign the .APK file using the following keystore details" option as Hein Andre Grønnestad mentioned.
The location was not correct.
I wanted to put a path that could work for everyone at work so I used $HOME/Library/... instead of /Users/Me/Library/...
The problem is $HOME doesn't mean anything special for java so the keystore file could not be found.
Solution: use relative path or absolute path without $HOME variable or ~

For me, it was a problem with my Keystore. Follow this link to enable diagnostics. This will give you a more detailed error message in the output window. If it says that your keystore failed to verify, you will have to fix your keystore. This solved the problem for me.

In my case solved the problem by signing the .APK whit the values signed into the keystore that we configured to create the APK file in Release mode.
I taked the Microsoft example in here
and here I put the same values
That way a solved my problem with
"java.exe" exited with code 2
But the real information problem was in output:
Failed to load signer "signer #1"
java.io.IOException: Invalid keystore format
At the moment I don't find the real solution to this problem but, this works.

Please Update your Visual Studio to Latest version, if you are still using an old version.
If you think there is no solution then go and enable the Multidex

In my case the one of keystore details was wrong, after saving-> cleaning -> building process it worked.

Try reset the keystore <AndroidSigningKeyStore> or change to false the key
>
<AndroidKeyStore>true</AndroidKeyStore>
Only to check. But remembering that it needs to be set to true.
After Clean and Rebuild the solution.

For me it turned out I had Eclipse Temurin JDK installed and that was being used. In VS Tools>Options>Xamarin I changed the path to Java Development Kit Location to from the Eclipse path to C:\Program Files\Microsoft\jdk-11.0.12.7-hotspot.
Maybe more importantly, I discovered Eclipse Temurin JDK was being used by running:
msbuild /bl /t:"Restore;SignAndroidPackage"
in my solution dir and then opening the resulting msbuild.binlog and clicking on the few errors and a path with Eclipse stood out. Your problem may be different but finding it this way may be the key to solving it.
EDIT: My settings don't stick after closing and reopening VS 2019. At this point I started using VS 2022 and don't have the issue.

Note: Multi-dex is enabled.
After 5 hours of looking or an answer, this is the conclusion I've found -
Enable diagnostic MSBuild output within Visual Studio, so you can see more details about your error:
Click Tools > Options...
In the left-hand tree view, select Projects and Solutions > Build and Run
In the right-hand panel, set the MSBuild build output verbosity dropdown to Diagnostic, Click OK
Clean and rebuild your package. Diagnostic output is visible within the Output panel.
If your error shows "java.io.IOException: Invalid keystore format", you are probably using an outdated Java sdk file, so do next:
Open Visual Studio and update the Java Development Kit Location to
point to the new JDK under Tools > Options > Xamarin > Android
Settings > Java Development Kit Location:
Be sure to restart Visual Studio.

My experience is...
I updated the following elements:
Xamarin.Android.Support.Design
Xamarin.Android.Support.v4
Xamarin.Android.Support.v7.AppCompat
Xamarin.Android.Support.v7.CardView
Xamarin.Android.Support.v7.MediaRouter
from 26.1.0.1 to 28.0.0.3
Then, after it started the issue with:
"java.exe" exited with code 2 Xamarin.Android project.
I just ticked the Enable Multi-Dex inside the Android project property. I am using VS 2017 in windows system.

Related

Android Studio - The process cannot access the file because it is being used by another process [duplicate]

I can import a sample app project, try to build it and I keep getting this error. Not always the same file but a new temp file each time. I've tried several different samples. I've been programming for 30+ years and feel completely clueless. Only clue seems to be the Windows Task Manager shows 99% to 100% CPU usage when it gives the error and ends the build.
Do This
File -> Invalidate Caches / Restart.. -> Invalidate/ Restart
When this happens to me on Windows 8, I open the task manager
and simply kill all the instances of "openjdk platform binary"
Just clean the project and have a run ! It would work ....
Choose app instead of DefaultPreview. Now if you build, then you should not see this issue.
Invalidating the IDE cache is NOT enough.
To delete the folder app\build we should first stop locking process - OpenJDK Platform binary.
Delete the folder app\build
Invalidate Caches / Restart...
Context: Using Coroutines in Android Instrumented Test.
GL
I have the same issue's.Even I tried all the solution mentioned here it didn't work.Even I can't delete the signin file as running android studio administrative mode.I see a notification at bottom right side of the IDE update gradle pluggin.So update gradle pluggin usually worked for me
Kill task "openjdk platform binary" works for me
Restart of Android Studio with cache invalidation doesn't help.
As others said, we should delete a <module>\build folder. Because a file is locked by OpenJDK Platform binary, we should stop this process in Task Manager (or unlock the file via LockHinter).
I had the same problem and in my case it is due to multiple Java Run times (JRE's). Try switching between JRE's in android studio or use only one Java runtime.
Hope it fix the problem. Happy Coding..
Invalidate Caches / Restart doesn't work all the time and it's not the better idea to close the whole Android Studio just we couldn't figure out what process was using it.
Please follow the below steps to figure out which process is using your file.
You can use the Resource Monitor for this which comes built-in with Windows 7, 8, and 10.
Open Resource Monitor, which can be found
By searching for Resource Monitor or resmon.exe in the start menu, or
As a button on the Performance tab in your Task Manager
Go to the CPU tab
Use the search field in the Associated Handles section
When you've found the handle, you can identify the process by looking at the Image and/or PID column.
You can then try to close the application as you normally would, or, if that's not possible, just right-click the handle and kill the process directly from there.
Ref : https://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows
try to delete the file (the folder inside builder message)
the message will contain process name ( i dont remember the name)
find the prcess in task manager and stop it
I had 2 jre's installed when I installed android studio. Uninstall the one that you find from control panel and that is located something like c:\program files\java etc.
On the next opening of android studio 4.0 + it will fallback to "C:\Program Files\Android\Android Studio\jre" and the process lockdown after each succesfull build will stop
If the error is recurring, you can try a file leak detector to determine what part of the gradle process is holding your files. As suggested here,
Download file leak detector
Check with resmon what process is holding on to the file
Depending on where the file is locked, do the following:
Android Studio: In studio, go to Help → Edit custom VM options and add the following line:
-javaagent:path/to/file-leak-detector.jar=http=19999
Gradle: Add to your gradle.properties file the following line:
org.gradle.jvmargs=-javaagent:path/to/file-leak-detector.jar=http=19999
When the problem occurs again, open localhost:19999 in your browser and search for locked files.
In my case, I found out that a file was held by AspectJ compiler/weaver (ajc):
#115 C:\Users\me\StudioProjects\myapp\app\build\intermediates\compile_and_runtime_not_namespaced_r_class_jar\debug\R.jar by thread:Execution worker for ':' Thread 3 on Mon Oct 19 18:41:06 BST 2020
at java.util.zip.ZipFile.<init>(ZipFile.java:156)
...
at org.aspectj.tools.ajc.Main$run.call(Unknown Source)
I was running it directly from Gradle:
new Main().run(args, handler)
I don't know if it's possible to make ajc release the files. So I resolved my issue by running it in a separate process. I added a new configuration to get aspectjtools onto classpath:
val weaving: Configuration by configurations.creating
dependencies {
weaving("org.aspectj:aspectjtools:1.9.6")
}
And instead of the above code, I did:
javaexec {
classpath = weaving
main = "org.aspectj.tools.ajc.Main"
args = arguments
}
For those which are still searching for this answer in 2022, remove the build folder and rebuild...
Had the same on Windows 10, it was resolved by closing all "Command Prompt" (cmd) windows 🤷‍♂️
Terminating "OpenJDK platform binary" from Task Manager then deleting src/build and finally rebuilding the project should make it work.
Above suggested solutions may fixed this error but they are time consuming so the best solution to fix this problem is open command prompt and run the following command.
taskkill /im java.exe /f
In my case, deleting the /.gradle/ directory and rebuilding resolved the issue.
If you are using External Emulator:
Restarting Android Studio with cache invalidation doesn't helped.
The file that caused the problem is being used by the emulator and if you are using an external emulator (BlueStacks) like me, restarting Android Studio will not help. I restarted the emulator and the problem went away.
It happened me many times while building or Cleaning the Project
The solution is simple:
Open the Specific Directory
Close the Android Studio
Delete the particular Directory
Now run the Android Studio

Why is Visual Studio trying to use the wrong JDK version?

I am trying to build WeatherApp, which comes from this tutorial, but I am seeing this error:
The specified task executable location "C:\Program Files (x86)\Java\jdk1.7.0_55\bin\javac.exe" is invalid.
and this warning (not sure if they are related):
Found conflicts between different versions of the same dependent assembly. In Visual Studio, double-click this warning (or select it and press Enter) to fix the conflicts; otherwise, add the following binding redirects to the "runtime" node in the application configuration file: <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Runtime" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" /></dependentAssembly></assemblyBinding>
I can doube-click the warning to make it go away, but the error remains. I am not sure why the project is trying to use that version of the jdk, as I have changed my xamarin optioins to point at Program Files instead:
Why is Visual Studio trying to use that versioin of the JDK?
Sometimes there's a sdk.caches file that will hold an old reference. Please try to delete both your obj/bin folders and see if that resolves the issue. If it doesn't, turn up your Build Output verbosity and attach a Build Output Log to your post:
https://forums.xamarin.com/discussion/27515/how-to-obtain-diagnostic-build-logs (XS)
http://blogs.msdn.com/b/msbuild/archive/2005/09/29/475157.aspx (VS)

Missing a “Permissions” manifest attribute in Java

I have some problems with opening a SAN switch in Webtools (Java version 8 Update 31).
The first problem was a failed to validate certificate
This was fixed by following the instructions on this site
However, then I got following error:
Which was solved by adding these 3 entries to the site to the exception site list (the last one is the only one necessary I think)
http://i.p.address/*
http://i.p.address/switchExplorer.html
http://i.p.address/
But now I get following error:
your security settings have blocked an application from running due to missing a “permissions” manifest attribute in the main jar web tools
The solution seems to be adding the URL to the exceptions but I have already done that in the previous step..
Open the "Mission Control Panel" of java.
If you cannot find it, have a look in the bin folder of your jdk installation, the executable should be called jcontrol.
Run it, open tab "Security", Button "Edit Site List" and add your URL to the Exception Site List. I.e. https://10.0.0.1 is all you need.
Use oracle java if you use linux and somehow miss jcontrol from the openjdk package.
Afterwards you should be able to start the .jnlp (or however you want to start the applet) of your desire.

libGDX Example Project in Eclipse Getting Error 87

I was trying out the sample project using libGDX(a clone of Flappy Bird) in Eclipse. This is the link to the tutorial - http://www.kilobolt.com/day-2-setting-up-libgdx.html.
In the step 18(please visit the link), when I clicked the "Run as Java Application", I am getting error alert box with message "LoadLibrary failed with error 87:The parameter is incorect". What could be the reason?
ok even though i didn't understand the reason, just got the solution from here http://aerotekit.com/win8-1-loadlibrary-failed-error-87-parameter-incorrect. This is an issue with windows 8.1 (my assumption) and it can be solved by editing the registry. Copy-pasting the solution below:
Copy this code into a text file:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class{4d36e968-e325-11ce-bfc1-08002be10318}\0000]
"OpenGLVendorName"=hex(7):61,00,74,00,69,00,6f,00,36,00,61,00,78,00,78,00,2e,\
00,64,00,6c,00,6c,00,00,00,61,00,74,00,69,00,6f,00,36,00,61,00,78,00,78,00,\
2e,00,64,00,6c,00,6c,00,00,00,00,00
Now save the file and change the extension from .txt to .reg. If you are unable to edit the extension you may have to go into your folder options and make the extensions viewable.Note: The next step has you edit a registry entry. I recommend that you make a registry backup before proceeding. While this is unlikely to cause any issues, it is better to be safe than sorry.
After you have the .reg file on your desktop just double click it and it will run.
Restart your computer.
I got the same error when I was trying an example from book. I changed the jdk used in eclipse from 64 bit to 32 bit and this error was gone.

ClassNotFoundException with static context class [duplicate]

I write a android app in Eclipse and use run command on my cellphone.
It's run success on my cellphone.
But I use [Android Tools -> Export Signed Application Package...] to export my app.
Then use [adb install] to install apk on my cellphone and run.
And I got the error message:
07-22 11:20:43.537: E/AndroidRuntime(20216): Caused by: java.lang.ClassNotFoundException: com.mytest.MyActivity in loader dalvik.system.PathClassLoader[/data/app/com.mytest-1.apk]
In [Java Build Path -> Order and Export], I have been selected all.
In AndroidManifest.xml, the application attribute doesn't have android:name.
I don't know how to solve this problem.
I'm success use Eclipse run on my cellphone, why run error when I export the app?
please help me, thanks!!
I had same issue, after 2 hours of research I managed to fix this problem by deleting "bin" and "gen" folders and rebuilding project.
There seems to multiple reasons for this happening from what I have researched. Do you have a lib folder? if so change it to "libs".
Do you have proguard enabled? If so that may cause the problem.
Also try unchecking the "Android #.#.#" and "Android Dependencies" in your Build Path, Order and Export.
Check out this thread which has a bunch of other possible solutions, a lot of them deal with setting the proper ActivityName in the manifest file.
java.lang.ClassNotFoundException on working app
If those solutions still don't solve the problem try these threads.
Android: Unable to instantiate activity / ClassNotFoundException
ClassNotFoundException Android
What I had to do is just re-do the building/exporting process with no modification to the code whatsoever. It worked for me, which I did was to export it two times.
On my first export, the build APK has lower file size and the second export (with no any modification to the code/configuration) produced a slightly larger file size (about 200KB diff).
This bug in building is really costly, kills you in front of your manager, and should be really fixed by Android/Eclipse team. Kind of sucks

Categories