java.exe finished with non-zero exit value 1

2019-07-09 03:47发布

问题:

Just for start, I am not rly trying to read cell(s) from .xlsx file in Android, I already tried almost everything what I Googled, but every time (on two different PCs, both Java 1.7.0_79) when I'm trying to build (run) this app, it ends with same error:

Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 1

I have no idea, what more should I do, Java and Android Studio already reinstalled... Nothing. I used code from https://github.com/hmkcode/Java/tree/master/java-excel-poi

Full output from Messages

Information:Gradle tasks [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:assembleDebug]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72310Library UP-TO-DATE
:app:prepareComAndroidSupportMultidex100Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42310Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl UP-TO-DATE
:app:processDebugAndroidTestManifest UP-TO-DATE
:app:compileDebugAndroidTestRenderscript UP-TO-DATE
:app:generateDebugAndroidTestBuildConfig UP-TO-DATE
:app:generateDebugAndroidTestAssets UP-TO-DATE
:app:mergeDebugAndroidTestAssets UP-TO-DATE
:app:generateDebugAndroidTestResValues UP-TO-DATE
:app:generateDebugAndroidTestResources UP-TO-DATE
:app:mergeDebugAndroidTestResources UP-TO-DATE
:app:processDebugAndroidTestResources UP-TO-DATE
:app:generateDebugAndroidTestSources UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources UP-TO-DATE
:app:dexDebug
trouble processing "javax/xml/stream/events/StartElement.class":
Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.
This is often due to inadvertently including a core library file
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is the most likely explanation of what's
going on.
However, you might actually be trying to define a class in a core
namespace, the source of which you may have taken, for example,
from a non-Android virtual machine project. This will most
assuredly not work. At a minimum, it jeopardizes the
compatibility of your app with future versions of the platform.
It is also often of questionable legality.
If you really intend to build a core library -- which is only
appropriate as part of creating a full virtual machine
distribution, as opposed to compiling an application -- then use
the "--core-library" option to suppress this error message.
If you go ahead and use "--core-library" but are in fact
building an application, then be forewarned that your application
will still fail to build or run, at some point. Please be
prepared for angry customers who find, for example, that your
application ceases to function once they upgrade their operating
system. You will be to blame for this problem.
If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
you in this endeavor. If you find that you cannot do this, then
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.
1 error; aborting
Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 1
Information:BUILD FAILED
Information:Total time: 2.301 secs
Information:1 error
Information:0 warnings
Information:See complete output in console

MainActivity.java

package tona_kriz.kriziksupl;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class MainActivity extends Activity {


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Workbook wb = null;

        //1. Open the file

        try {
            AssetManager assManager = getApplicationContext().getAssets();
            InputStream is = null;
            try {
                is = assManager.open("supl_zak.xlsx");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            InputStream caInput = new BufferedInputStream(is);
            wb = new XSSFWorkbook(caInput);
            int i = wb.getNumberOfSheets();
            Toast.makeText(getApplicationContext(), "pocet: " + i, Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "otevreno", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //2. Open a sheet
        Sheet sheet = wb.getSheetAt(0);


        //3. Get each cell by row & column number
        Cell cell;
        //Cell cell = sheet.getRow(0).getCell(0);
        //double numberVal = cell.getNumericCellValue();
        //System.out.println("Row: 0 - Column: 0 = "+numberVal);
        //-----------------------------
        cell = sheet.getRow(0).getCell(8);
        String stringVal = cell.getStringCellValue();
        System.out.println("Row: 0 - Column: 1 = "+stringVal);
        //-----------------------------
        //cell = sheet.getRow(0).getCell(2);
        //Date dateVal = cell.getDateCellValue();
        //System.out.println("Row: 0 - Column: 2 = "+dateVal);
        //-----------------------------
        //cell = sheet.getRow(0).getCell(3);
        //boolean booleanVal = cell.getBooleanCellValue();
        //System.out.println("Row: 0 - Column: 3 = "+booleanVal);
        //-----------------------------

    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tona_kriz.kriziksupl" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:largeHeap="true"
        android:theme="@style/AppTheme"
        android:name="android.support.multidex.MultiDexApplication"> >
        <activity android:name=".MainActivity" >
            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    dexOptions {
        preDexLibraries false
        incremental true
        javaMaxHeapSize "4g"
    }

    afterEvaluate {
        tasks.matching {
            it.name.startsWith('dex')
        }.each { dx ->
            if (dx.additionalParameters == null) {
                dx.additionalParameters = ['--multi-dex']
            } else {
                dx.additionalParameters += '--multi-dex'
            }
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

    defaultConfig {
        applicationId "tona_kriz.kriziksupl"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        multiDexEnabled false
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:multidex:1.0.0'
    compile files('libs/commons-codec-1.5.jar')
    compile files('libs/dom4j-1.6.1.jar')
    compile files('libs/poi-3.9.jar')
    compile files('libs/poi-ooxml-3.9.jar')
    compile files('libs/poi-ooxml-schemas-3.9.jar')
    compile files('libs/stax-api-1.0.1.jar')
    compile files('libs/xml-apis-1.0.b2.jar')
    compile files('libs/xmlbeans.jar')
}

But, when I remove:

compile files('libs/commons-codec-1.5.jar')
compile files('libs/dom4j-1.6.1.jar')
compile files('libs/stax-api-1.0.1.jar')
compile files('libs/xml-apis-1.0.b2.jar')
compile files('libs/xmlbeans.jar')

the app starts, but crashes...

Logcat http://pastebin.com/NG3AnW3z

File "supl_zak.xlsx" can be downloaded from here.

Libraries

Is there any solution for me?

回答1:

Unfortunately you cannot directly use Apache POI on Android due to third party library dependencies that are pulled in. Also the size of the library causes a very big application file, which is often not possible/desirable. You even sometimes exceed limits imposed by the Dalvik Android Bytecode compilation.

There are some resources available which discuss how to get POI running on Android:

  • https://github.com/andruhon/android5xlsx - repackaged jars with duplicate packages in XMLBeans removed
  • http://blog.kondratev.pro/2015/08/reading-xlsx-on-android-4-and-hopefully.html
  • Apache POI with Android -- How to Create ,Read , Write, Delete PowerPoint Presentations?


回答2:

The reason is that at least one the libraries (xmlbeans.jar) is declaring classes that may conflict with the core libraries that Android uses.

xmlbeans declares classes in a javax.xml package - Android also has classes in this package and other similar ones.

From the error message:

If you are legitimately using some code that happens to be in a core package, then the easiest safe alternative you have is to repackage that code. That is, move the classes in question into your own package namespace.

Basically, the build tools think that you should be building a core library, but instead, you are attempting to build an application using a library which conflicts with the default Android core library. It suggests you move your code from the core package into a different package.

My advice - stop using external libraries that attempt to use core package names and use the XML functionality that Android provides.



回答3:

I have an idea you can follow: some jars you import are internal API, like layoutlibs.jar, they are only involved in compiling, not packaged in apk.If you want to use them ,just create a folder named mylib in the root of the module ,then look the picture,follow them:

If my answer solve your problem ,please adopt it.



回答4:

Problem: java.exe finished with non-zero exit value 1

Solution: For solving this problem I have tried most of the solutions and hit and try again and again.

At last found the solution to this problem.

These are possible solutions you should try for solving your problem and according to me, it will definitely solve your issue.

  1. Clean and Rebuild your Project.
  2. Please check all the libraries are updated and gradle build version also updated.
  3. try to increase your java heap size.
  4. Don't use lambda expressions or syntax in your code. in my case that is the problem because I was using lambda expressions and when I was using lambda expression then it needs a declaration in build.gradle (app)file like

compileOptions { targetCompatibility 1.8 sourceCompatibility 1.8 }

this.

it is the actual problem which I have faced please either remove this compileOptions declaration from build.gradle file or use the proper version which is compatible with your Gradle version.

Thanks

I hope this solution will help you and make your problem resolvable.