Java code running on windows but not on linux

1k views Asked by At

I have this code where I am trying to run some piece of code on windows , which is running fine as I am getting the desired output

package com.org.jato.expressionTransform;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class ExpressionTransformerImpl {

    private String delimiterForForumal = "_V_";

    private String delimiterForId = "_VSCAM_";

    private int rowLimit = 50000;

    private Set<String> usedCellIdsSet = new HashSet();

    public void convertCurlyBraceToOrCondition(String formula, Long customerId,
            Long userId) {

    }

    public List<String> DelimitFormula(String formula, Long loggedInUserId,
            Long customerId) {
        List<String> updatedForumala = new ArrayList<String>();

        StringTokenizer tokenizer = new StringTokenizer(formula,
                delimiterForForumal);

        while (tokenizer.hasMoreTokens()) {
            updatedForumala.add(tokenizer.nextToken());
        }

        return updatedForumala;

    }

    public void createExecutableForumalaFromString(String forumlaString,
            Long customerId, Long loggedInUserId) {

        Set<String> idString = getIdsFromFormula(forumlaString);

        Map<String, Long> mappingIDItemIdMap = getItemIDsFromIdList(idString,
                1L, 2L);
        Set<Long> itemIdSet = new HashSet();
        for (Long val : mappingIDItemIdMap.values()) {
            itemIdSet.add(val);

        }
        Map<Long, Long> itemDataTypeMap = getDataTypeForItem(itemIdSet);

        Boolean isValid = isValidFormula(forumlaString, itemDataTypeMap, mappingIDItemIdMap);

        System.out.println(isValid);
    }

    private Set<String> getIdsFromFormula(String formulaString) {
        Set<String> idList = new HashSet();

        StringTokenizer idTokens = new StringTokenizer(formulaString,
                delimiterForId);

        while (idTokens.hasMoreTokens()) {
            idTokens.nextToken();

            if (idTokens.hasMoreTokens()) {
                idList.add(idTokens.nextToken());
            }
        }

        return idList;
    }

    private Map<String, String> getCellNamesForIDs(Set<String> idStrings) {
        Map<String, String> idCellMap = new HashMap();

        int rowNum = 1;
        int colNum = 0;
        String array[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
                "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                "W", "X", "Y", "Z" };

        List<String> columnLIst = new ArrayList<String>(Arrays.asList(array));

        for (String id : idStrings) {

            String key = columnLIst.get(colNum) + rowNum;
            if (!usedCellIdsSet.contains(key)) {
                idCellMap.put(key, id);
                usedCellIdsSet.add(key);
            }
            rowNum++;
            if (rowNum == rowLimit) {
                rowNum = 1;
                colNum = colNum + 1;
            }

        }

        return idCellMap;
    }

    private Map<String, Long> getItemIDsFromIdList(Set<String> idList,
            Long speedWingcustId, Long loggedInUserId) {
        Map<String, Long> map = new HashMap();
        for (String id : idList) {
            Long var = Long.valueOf(id) % 5L;
            if (var == 0L) {
                map.put(id, 5L);

            } else {
                map.put(id, var);
            }

        }
        return map;
    }

    private Map<Long, Long> getDataTypeForItem(Set<Long> itemIDList) {
        Map<Long, Long> map = new HashMap();
        for (Long id : itemIDList) {
            Long var = Long.valueOf(id) % 5L;
            if (var == 0L) {
                map.put(id, 5L);

            } else {
                map.put(id, var);
            }

        }
        return map;

    }

    public Boolean isValidFormula(String formulaString,
            Map<Long, Long> itemDataMap, Map<String, Long> itemAttributeIdMap) {
        Boolean isValid = Boolean.FALSE;

        for (String mappingID : itemAttributeIdMap.keySet()) {
            formulaString = formulaString.replaceAll(delimiterForId + mappingID
                    + delimiterForId, itemAttributeIdMap.get(mappingID)
                    .toString());
        }

        for (Long itemID : itemDataMap.keySet()) {
            formulaString = formulaString.replaceAll(itemID + ".value",
                    getReplacement(itemDataMap.get(itemID)));
            formulaString = formulaString.replaceAll(itemID + ".availability",
                    "S");
        }

        System.out.println(formulaString);
        isValid = getDummyResultForFormul(formulaString);
        return isValid;
    }

    private String getReplacement(Long dataType) {
        switch (dataType.intValue()) {
        case 1:
            return "1";
        case 2:
            return "A";
        default:
            break;
        }
        return null;

    }

    private boolean getDummyResultForFormul(String formula) {
        Boolean isValid = Boolean.TRUE;
        Object workbook = null;
        Variant f = null;

        ActiveXComponent xl = new ActiveXComponent("Excel.Application");
        Object xlo = xl.getObject();
        try {
            xl.setProperty("Visible", new Variant(true));
            Object workbooks = xl.getProperty("Workbooks").toDispatch();
            workbook = Dispatch.get((Dispatch) workbooks, "Add").toDispatch();
            Object sheet = Dispatch.get((Dispatch) workbook, "ActiveSheet")
                    .toDispatch();

            Object a1 = Dispatch.invoke((Dispatch) sheet, "Range",
                    Dispatch.Get, new Object[] { "A1" }, new int[1])
                    .toDispatch();
            Dispatch.put((Dispatch) a1, "Formula", formula);
            String val = Dispatch.get((Dispatch) a1, "Value").toString();
            f = new Variant(false);
            Dispatch.call((Dispatch) workbook, "Close", f);
            if(val.equals("null"))
            {
                isValid = Boolean.FALSE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            isValid = Boolean.FALSE;
            f = new Variant(false);
            Dispatch.call((Dispatch) workbook, "Close", f);
        } finally {
            xl.invoke("Quit", new Variant[] {});
        }

        return isValid;
    }

    public static void main(String args[]) {

        String formula = "=IF(_VSCAM_1111_VSCAM_.value<5,IF(_VSCAM_2222_VSCAM_.value={\"high mech\",\"low mech\",\"mid mech\"},10,IF(_VSCAM_2222_VSCAM_.value=\"electronic\",20,0)),IF(_VSCAM_1111_VSCAM_.value=5,IF(_VSCAM_2222_VSCAM_.value=\"electronic\",40,0),0))";

        ExpressionTransformerImpl test = new ExpressionTransformerImpl();

        test.createExecutableForumalaFromString(formula, 1L, 2L);

    }
}

On running on linux, I am getting below error.

Exception in thread "main" java.lang.NoClassDefFoundError: com/jacob/activeX/ActiveXComponent
        at ExpressionTransformerImpl.getDummyResultForFormul(ExpressionTransformerImpl.java:183)
        at ExpressionTransformerImpl.isValidFormula(ExpressionTransformerImpl.java:161)
        at ExpressionTransformerImpl.createExecutableForumalaFromString(ExpressionTransformerImpl.java:60)
        at ExpressionTransformerImpl.main(ExpressionTransformerImpl.java:221)
Caused by: java.lang.ClassNotFoundException: com.jacob.activeX.ActiveXComponent
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

to compile this , I am using the below command

 javac -cp  jacob.jar ExpressionTransformerImpl.java

and to run this , I am using this command

java ExpressionTransformerImpl

how do I fix this issue?

any help would be much appreciated.

2

There are 2 answers

4
BetaRide On BEST ANSWER

You are out of luck. JACOB requires a jacob.dll file which only works on windows.

2
Marged On

ActiveX is not available on Linux, as it (like jacob) needs Windows binaries. I think even using an emulator like Wine will not help because running Excel on Wine is not the easiest.

Perhaps the best way is to switch to something platform independent for creating the XLS file: https://poi.apache.org/

I don't provide an example because I am sure there a lots of to be found using google or stackoverflow.