I am currently using vscode and apache poi, created a program to automatically create a .xlsx program and have A1 cell input a String called "Tester", and that error pops up.
Codes in my program:
package excel_reader;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelWriter {
public static void main (String[] args) throws Exception {
//ExcelReader eR = new ExcelReader();
XSSFWorkbook workbook = new XSSFWorkbook(); // here is the7 line 13
// first sheet create
XSSFSheet sheet = workbook.createSheet("FirstExcelSheet");
// first row create - A
XSSFRow row = sheet.createRow(0);
// first cell create - 1
XSSFCell cell = row.createCell(0); // A-1
// give data into A-1 cell
cell.setCellValue("Tester");
// Output as an excel file
workbook.write(new FileOutputStream("C:\\Users\\Sonic\\Desktop\\book.xlsx"));
workbook.close();
// C:\\Users\\Sonic\\Desktop\\book.xlsx
}
}
Error Code:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell aka.ms/pscore6
PS E:\excel_reader_v.1_beta1-0> cd 'e:\excel_reader_v.1_beta1-0'; & 'C:\Users\Sonic\.vscode\extensions\vscjava.vscode-java-debug-0.24.0\scripts\launcher.bat' 'C:\Users\Sonic\AppData\Local\Programs\AdoptOpenJDK\bin\java' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Sonic\AppData\Local\Temp\cp_5wuvlu562pjj6rfd2pconxvl4.jar' 'excel_reader.ExcelWriter'
Exception in thread "main" java.lang.NoSuchFieldError: DEFAULT
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:161)
at excel_reader.ExcelWriter.main(ExcelWriter.java:13)
pom.xml (dependency):
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
Apache POI is new to me, please do help me, I will be very appreciated, thank you very much.
Cause of java.lang.NoSuchFieldError
If you search StackOverflow for tag nosuchfieldexception you will find a lot of similar questions. Most of them have 2 things in common:
the error says that a CONSTANT field could not be found
the cause is that 2 or more dependencies (i.e. third-party JAR files) which are related have different version.
This version mismatch may also cause your error:
java.lang.NoSuchFieldError: DEFAULT
Since the constant DEFAULT of class org.apache.poi.ss.formula.udf.UDFFinder existed in versions prior to 3.17, but now (versions 4.0 and newer) has been deprecated. Means, it does not exist in version 4.0 and above.
Problem at runtime:
If statement new XSSFWorkbook(); (at line 13) is executed using dependency (JAR) poi-ooxml, then it uses a UDFFinder of the core dependency (JAR) poi and thus tries to find the CONSTANT field DEFAULT (which may not be there, since versions mismatch).
So I suppose, your versions of Apache poi and poi-ooxml are mismatching (not the same level).
See similar question with version mismatch using Apache Poi.
Solution: matching versions!
If you are using Maven, make sure you have these dependencies on your POM:
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
At least both have to be the same version!
If you are managing your classpath and JARs manually, watch out for the filename of all apache-poi JAR files and make sure the versions are same.
Code seems very close to working
If you just want to write your freshly created Excel-Workbook to a file, e.g. /tmp/MyFirstExcel.xlsx (on Linux) or C:/Users/Sonic/MyFirstExcel.xlsx (on Windows, supposed your Windows-User is Sonic) then use this code:
public class ExcelWriter {
static final String FILE_PATH_LINUX = "/tmp/MyFirstExcel.xlsx";
static final String FILE_PATH_WIN = "C:/Users/Sonic/MyFirstExcel.xlsx"; // note: replace Window's backslash '\' by either double-backslash '\\' or single forward-slash '/'
public static void main(String[] args) throws FileNotFoundException, IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("FirstExcelSheet"); // first sheet create
XSSFRow row = sheet.createRow(0); // first row create - A
XSSFCell cell = row.createCell(0); // first cell create - A-1
cell.setCellValue("Tester"); // give data into A-1 cell
// Output as an excel file
workbook.write(new FileOutputStream(FILE_PATH_WIN));
workbook.close();
}
}
Unclear usage of class ExcelReader:
There is no obvious reason to extend from ExcelReader.
Thus and for simplicity you can omit the extends ExcelReader, declaration of ExcelReader eR = new ExcelReader(); and usage eR.getImpoFileLink() to get the file-path. Otherwise please post the source-code of class ExcelReader to your question, to make it a minimal reproducible example.
Minimal working solution (above) explained:
Instead just put a file-path to desired folder and file, like constant FILE_PATH_WIN here.
I saw your related question few hours ago, where you did that.
Supposed you fixed your other issues/questions here, as well as resolved the version-mismatch with POI dependencies here, the solution given above should compile and write out the desired Excel workbook (.xlsx) with sheet FirstExcelSheet and cell A1 containing string Tester.
See also
See this tutorial on Apache POI – Reading and Writing Excel file in Java.
Because putting this in the comment would not be feasible. Could you try to remove this portion of code in the pom.xml and try it again?
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
based on cursory reading. it has been defined in the bellow code
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
The only apache-poi dependencies you need in pom.xml are
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0</version>
</dependency>
Please remove others.
When I removed the others, my code worked.
Related
deeplearning4j returning "Expected model class name Model (found Functional)." when trying to load a keras model.
same happens when try to load it as sequential.
python :
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = ResNet50(weights='imagenet')
model.save("resnet50.h5")
java(spring)
public INDArray useModel(String name, Long id) throws Exception{
File file= new File("src/main/uploads/image.jpg");
String resnet = "src/main/uploads/model/resnet50.h5";
ComputationGraph model = KerasModelImport.importKerasModelAndWeights(resnet);
NativeImageLoader loader = new NativeImageLoader(224, 224, 3);
INDArray image = loader.asMatrix(file);
INDArray[] output = model.output(image);
return output[0];
}
mavn deps :
<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-modelimport</artifactId>
<version>1.0.0-beta7</version>
</dependency>
<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-core</artifactId>
<version>1.0.0-beta7</version>
</dependency>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native-platform</artifactId>
<version>1.0.0-beta7</version>
</dependency>
<dependency>
<groupId>org.datavec</groupId>
<artifactId>datavec-data-image</artifactId>
<version>1.0.0-beta7</version>
</dependency>
any help fixing the problem or guidence to another library would be appreciated,
I got the same problem when I tried to load my model either functional or sequentiel using DPL4J.
I solved the problem by downgrading tensorflow on python and re-train my model.
Then I used the loaded the file on Java.
I am using Java 8 and Dynamic reports version is 6.0.0.
In pom.xml, I have added this dependency :
<dependency>
<groupId>net.sourceforge.dynamicreports</groupId>
<artifactId>dynamicreports-core</artifactId>
<version>6.0.0</version>
</dependency>
This code is to generate the report parameters:
JasperReportBuilder report = new JasperReportBuilder();
report.setPageMargin(DynamicReports.margin().setLeft(30).setRight(30).setTop(10).setBottom(10));
report.setPageFormat(PageType.A4, PageOrientation.PORTRAIT);
report.title(cmp.subreport(subReport1));
Here, the variable subreport1 contains all the report data. They are verified to be correct.
After generating all the report data, this is the code I execute to write the report details into a file:
FileOutputStream st = new FileOutputStream("<<DUMMY_FILE_PATH>>");
report.toPdf(st);
At this line where .toPdf is present, the following exception is being thrown:
Exception in thread "main" java.lang.AbstractMethodError: javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V
at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.<init>(SimpleFontExtensionHelper.java:149)
at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.getInstance(SimpleFontExtensionHelper.java:131)
at net.sf.jasperreports.engine.fonts.FontExtensionsRegistry.ensureFontExtensions(FontExtensionsRegistry.java:80)
at net.sf.jasperreports.engine.fonts.FontExtensionsRegistry.getExtensions(FontExtensionsRegistry.java:57)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:130)
at net.sf.jasperreports.engine.DefaultJasperReportsContext.getExtensions(DefaultJasperReportsContext.java:277)
at net.sf.jasperreports.engine.fonts.FontUtil.getFontInfo(FontUtil.java:191)
at net.sf.jasperreports.engine.fonts.FontUtil.getFontInfo(FontUtil.java:291)
at net.sf.jasperreports.engine.fonts.FontUtil.getAwtFontFromBundles(FontUtil.java:476)
at net.sf.jasperreports.engine.fonts.FontUtil.getAwtFontFromBundles(FontUtil.java:466)
at net.sf.dynamicreports.design.transformation.StyleResolver.getFont(StyleResolver.java:122)
at net.sf.dynamicreports.design.transformation.StyleResolver.getFont(StyleResolver.java:100)
at net.sf.dynamicreports.design.transformation.StyleResolver.getFontHeight(StyleResolver.java:69)
at net.sf.dynamicreports.design.transformation.TemplateTransform.getTextFieldHeight(TemplateTransform.java:1391)
at net.sf.dynamicreports.design.transformation.ComponentTransform.textField(ComponentTransform.java:395)
at net.sf.dynamicreports.design.transformation.ComponentTransform.component(ComponentTransform.java:155)
at net.sf.dynamicreports.design.transformation.ComponentTransform.list(ComponentTransform.java:303)
at net.sf.dynamicreports.design.transformation.BandTransform.band(BandTransform.java:231)
at net.sf.dynamicreports.design.transformation.BandTransform.transform(BandTransform.java:86)
at net.sf.dynamicreports.design.base.DRDesignReport.transform(DRDesignReport.java:155)
at net.sf.dynamicreports.design.base.DRDesignReport.<init>(DRDesignReport.java:127)
at net.sf.dynamicreports.design.base.DRDesignReport.<init>(DRDesignReport.java:111)
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperReportDesign(JasperReportBuilder.java:299)
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.getJasperParameters(JasperReportBuilder.java:346)
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperPrint(JasperReportBuilder.java:363)
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.export(JasperReportBuilder.java:896)
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toPdf(JasperReportBuilder.java:735)
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toPdf(JasperReportBuilder.java:724)
Please let me know the solution for this error.
I found the actual solution. There was a version mismatch between xerces jar and dynamic reports jar. It was resolved by adding the following dependency :
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
</dependency>
Problem
I'm trying to produce some unicode characters after compiling my *.scss file.
As an example, I have the following (SCSS):
.element:after {
content: "\a0";
}
When the file is compiled, it outputs the following (CSS):
.element:after {
content: "\\a0";
}
Notice the extra unwanted backslash (\).
Attempted Solution #1
I did try the solution here: Sass: unicode escape is not preserved in .css file, which suggests introducing the following function:
#function unicode($str) {
#return unquote("\"")+unquote(str-insert($str, "\\", 1))+unquote("\"")
}
And using it like so (SCSS):
.element:after {
content: unicode("a0");
}
However, this produces the following (CSS)
.element:after {
content: "\\" ")+unquote(str-insert($str, " \\\\ ", 1))+unquote(" \\ "";
}
Notice, it's not even invoking the function as intended. Why is that?
Project Details
I'm using these libraries in Maven:
<dependency>
<groupId>net.jawr</groupId>
<artifactId>jawr-core</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>net.jawr.extensions</groupId>
<artifactId>jawr-spring-extension</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>com.darrinholst</groupId>
<artifactId>sass-java-gems</artifactId>
<version>3.4.20.0</version>
</dependency>
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-core</artifactId>
<version>9.1.5.0</version>
</dependency>
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-stdlib</artifactId>
<version>9.1.5.0</version>
</dependency>
Temporary solution
Don't use unicodes in SCSS. Instead, use Font Awesome in the HTML (keeping Font Awesome in a CSS file).
I had the same issue while trying to use a custom icons font. The solution I found is creating a mixin which looks like this :
#mixin class-for-icon($name, $code) {
.icon-#{$name}::before {
content: unquote("\"\\#{$code}\"");
}
}
And then I can use it like this :
#include class-for-icon('myIcon', 'e1ff');
/* generates */
.icon-myIcon::before {
content: "\e1ff";
}
I use node-sass 4.5.3 to compile my sass files into css, and this is working well (currently using it in production on a few projects)
Hope this helps !
I am trying to use an open source tool built on Batik and I am running into trouble with one of the dependencies when I try to build it. Pretty sure this is something to do with classpaths and library locations, but I can't figure out what is happening.
So the project I am working with ( SVG2EMF ) is using the FreeHep EMF Driver, which in turn uses the FreeHep GraphicsIO project. Because these three have not been playing nicely on my system ( Ubuntu 14.04 ) I've downloaded the source for all three to try and step through the problem.
Everything builds correctly and I can step through the code successfully, but the unit tests on SVG2EMF fail at the point where the EMF Driver makes a call to something from GraphicsIO- the relevant parts of the code in question is here:
import org.freehep.graphicsio.ImageGraphics2D;
import org.freehep.graphicsio.ImageConstants;
// ...snip...
public class AlphaBlend extends EMFTag implements EMFConstants
{
// ...snip...
public void write(int tagID, EMFOutputStream emf) throws IOException
{
emf.writeRECTL(bounds);
emf.writeLONG(x);
emf.writeLONG(y);
emf.writeLONG(width);
emf.writeLONG(height);
dwROP.write(emf);
emf.writeLONG(xSrc);
emf.writeLONG(ySrc);
emf.writeXFORM(transform);
emf.writeCOLORREF(bkg);
emf.writeDWORD(usage);
emf.writeDWORD(size); // bmi follows this record immediately
emf.writeDWORD(BitmapInfoHeader.size);
emf.writeDWORD(size + BitmapInfoHeader.size); // bitmap follows bmi
emf.pushBuffer();
int encode;
// plain
encode = BI_RGB;
ImageGraphics2D.writeImage(
(RenderedImage) image,
ImageConstants.RAW.toLowerCase(),
ImageGraphics2D.getRAWProperties(bkg, "*BGRA"),
new NoCloseOutputStream(emf));
// emf.writeImage(image, bkg, "*BGRA", 1);
// png
// encode = BI_PNG;
// ImageGraphics2D.writeImage(image, "png", new Properties(), new
// NoCloseOutputStream(emf));
// jpg
// encode = BI_JPEG;
// ImageGraphics2D.writeImage(image, "jpg", new Properties(), new
// NoCloseOutputStream(emf));
int length = emf.popBuffer();
emf.writeDWORD(length);
emf.writeLONG(image.getWidth());
emf.writeLONG(image.getHeight());
BitmapInfoHeader header = new BitmapInfoHeader(image.getWidth(), image
.getHeight(), 32, encode, length, 0, 0, 0, 0);
bmi = new BitmapInfo(header);
bmi.write(emf);
emf.append();
}
This throws a NoClassDefFoundError specifically relating to org.freehep.graphicsio.ImageGraphics2D on that writeImage call. When I step through in the debugger, a watch on ImageConstants.RAW has the value of Unknown type "org.freehep.graphicsio.ImageConstants" even though the application built quite happily with those references. Any references to ImageGraphics2D behave in exactly the same way.
The dependency in the SVG2EMF pom.xml looks like this:
<dependencies>
<!-- some other dependencies -->
<dependency>
<groupId>org.freehep</groupId>
<artifactId>freehep-graphicsio-emf</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
Dependency from the FreeHEP EMF Driver looks like this:
<dependencies>
<!-- necessary because transitive deps seem to go above inhertied deps -->
<dependency>
<groupId>org.freehep</groupId>
<artifactId>freehep-util</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.freehep</groupId>
<artifactId>freehep-graphicsio</artifactId>
<version>2.1.1</version>
</dependency>
<!-- Other dependencies -->
</dependencies>
Can anybody shed any light on what is actually going on here or what I need to be doing in order to enable this to work?
EDIT: I think I have found where the problem is coming from- way down the StackTrace I see a "Caused by: ExceptionInInitializerError" - which appears to mark the class as inaccessible from then on. So the dependency does exist, but an exception is being thrown by the initializer which causes the JRE to mark it as unusable.
Further Edit: To solve these problems it can be useful ( although it is not mentioned anywhere on the freehep.org website ) to know that the project is now hosted on Github so you can find newer versions from there. In my case going straight to the latest version solved the problem.
I am using StAX to create a quite large xml document. Until now I was using the IndentingXMLStreamwriter class to get a well formatted document (see also this answer). A few days ago we setup a jenkins server with an older jdk version (6.26), on which i get build errors.
package com.sun.xml.internal.txw2.output does not exist
I assume the package cannot be found because of the installed jdk version. For different reasons this cannot be changed
(by the way, does anyone know the jdk version, at which this package (com.sun.xml.internal.txw2.output) was added?).
Therefore I am looking for an alternative to do the indenting. I would prefer a solution similar to the one I was using, which means without reparsing the document. Any ideas or suggestions?
Thanks
Lars
Instead of com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter use com.sun.xml.txw2.output.IndentingXMLStreamWriter that can be found in:
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>txw2</artifactId>
<version>2.2.11</version>
</dependency>
Just expanding on Michael Kay's answer ( https://stackoverflow.com/a/10108591/2722227 ).
maven dependencies:
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>9.6.0-5</version>
</dependency>
java code:
import java.io.OutputStream;
import net.sf.saxon.Configuration;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.Serializer.Property;
public class Main {
public static void main(String[] args) throws Exception {
OutputStream outputStream = System.out;
writeXmlDocument(outputStream);
}
private static void writeXmlDocument(OutputStream outputStream){
Configuration config = new Configuration();
Processor processor = new Processor(config);
Serializer serializer = processor.newSerializer();
serializer.setOutputProperty(Property.METHOD, "xml");
serializer.setOutputProperty(Property.INDENT, "yes");
serializer.setOutputStream(outputStream);
try {
XMLStreamWriter writer = serializer.getXMLStreamWriter();
try {
writer.writeStartDocument();
{
writer.writeStartElement("root_element_name");
{
writer.writeStartElement("child_element");
writer.writeEndElement();
}
writer.writeEndElement();
}
writer.writeEndDocument();
writer.flush();
writer.close();
} catch (XMLStreamException e) {
e.printStackTrace();
}
} catch (SaxonApiException e) {
e.printStackTrace();
}
}
}
If other suggestions don't work, you can get an indenting XMLStreamWriter from Saxon like this:
Processor p = new net.sf.saxon.s9api.Processor();
Serializer s = p.newSerializer();
s.setOutputProperty(Property.METHOD, "xml");
s.setOutputProperty(Property.INDENT, "yes");
s.setOutputStream(....);
XMLStreamWriter writer = s.getXMLStreamWriter();
One advantage is that this allows you a lot of control over the serialization using other serialization properties.
There is an alternative implementation of IndentingXmlStreamWriter, which is provided as part of the open source stax-utils project here: http://java.net/projects/stax-utils/pages/Home
stax-utils seems to be a project set up to provide utilities based around the jsr-173 streaming xml api for Java
You'd need to add the stax-utils jar as a dependency for your project. Then you can import javanet.staxutils.IndentingXmlStreamWriter
Since stax-utils is in the maven central repository, if you use maven for your dependencies you can get it with:
<dependency>
<groupId>net.java.dev.stax-utils</groupId>
<artifactId>stax-utils</artifactId>
<version>20070216</version>
<exclusions>
<exclusion>
<groupId>com.bea.xml</groupId>
<artifactId>jsr173-ri</artifactId>
</exclusion>
</exclusions>
</dependency>
Functionality seems very similar / equivalent to the txw2 class
I have excluded jsr173-ri since I am using jdk 1.7. I think 1.6+ has the jsr173 api as a standard feature, but if you are using 1.5 or lower you'd need the extra jsr173 jar.
If you are using Maven and Java 8, you can import the following to use this class:
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.17</version>
</dependency>
And then, you import it as: import com.sun.xml.txw2.output.IndentingXMLStreamWriter;