I'm trying to generate and load from CSV filea and problem with CSVPrinter occured. The statement is the following:
Cannot resolve constructor 'CSVPrinter(java.io.BufferedWriter,org.apache.commons.csv.CSVFormat)"
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import java.io.BufferedWriter;
import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(file.getAbsolutePath()));
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
.withHeader("title", "description", "priority"))
) {
for (int i = 0; i < serializer.toDO.size(); i++){
csvPrinter.printRecord(serializer.toDO.get(i).getTitle(),
serializer.toDO.get(i).getDescription().replace("\n"," "),
serializer.toDO.get(i).getPriority(),
serializer.toDO.get(i).getLocalDate(),
"toDo");
}
Assuming you are using the apache variant, your code worked for me.
I think you need to add some of the details that you assure us you have done. My guess is that you have the wrong library in your project/classpath.
In what environment are you running your code (within the IDE or standalone from the command prompt)?
How did you incorporate the library (download JAR directly from Apache or automatically via a service - e.g. maven)?
Here is my version of the source code:
package csvwriter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVFormat;
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Main m = new Main();
m.go(args);
}
public void go(String [] args) {
File file = new File (args[0]);
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(file.getAbsolutePath()));
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
.withHeader("title", "description", "priority"))
) {
System.out.println("Done.");
// for (int i = 0; i < serializer.toDO.size(); i++){
// csvPrinter.printRecord(serializer.toDO.get(i).getTitle(),
// serializer.toDO.get(i).getDescription().replace("\n"," "),
// serializer.toDO.get(i).getPriority(),
// serializer.toDO.get(i).getLocalDate(),
// "toDo");
// }
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Here are my libraries:
I downloaded the library from the apache download center
When I run, I get the following as the output:
run:
Done.
BUILD SUCCESSFUL (total time: 0 seconds)
Repeating this exercise in IntelliJ (Java Project, incorporate apache-commons-csv-1.6) via Maven, produces the same result:
"C:\Program Files\Java\jdk1.8.0_172\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1\lib\idea_rt.jar=59482:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_172\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_172\jre\lib\rt.jar;C:\cygwin64\home\gm310509\Projects\Learning\Miscellaneous\CSVPrinter\out\production\CSVPrinter;C:\cygwin64\home\gm310509\Projects\Learning\Miscellaneous\CSVPrinter\lib\commons-csv-1.6.jar" stackoverflow.Main c:\temp\resistor.html
Done.
Process finished with exit code 0
Here is the "add library via maven" dialog:
Related
I am trying to run the java program on command line but this message is shown:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
Then I have searched a few hours and most solutions are about checking the
Java version,
importing the JDBC jar and
editing the classpath
For 1, I checked the java version I am using is 1.8
So for 2, I have downloaded the sqljdbc41.jar , put the jar into a lib folder, and run the command with:
java -cp C://Testing/lib/* -classpath . sampleJava
For 3, I have edited the classpath to C://Testing/lib/sqljdbc41.jar
Anyone knows do I miss something so that the same error message still shows?
Below is the sample program I run:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
public class sampleJava {
int x = 5;
public static void main(String[] args) {
sampleJava myObj = new sampleJava();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
System.err.println(e);
System.out.println(e.toString());
e.printStackTrace();
}
finally {
System.out.println("Done");
}
System.out.println(myObj.x);
}
}
This should help
java -cp C:/Testing/lib/*; sampleJava
or
java -cp C:/Testing/lib/sqljdbc41.jar; sampleJava
document4j looks like a great api and I'd love to use it. I just want to bulk convert docx to pdf on my mac (with Microsoft office installed).
I have written this but I get the error that the LocalConverter cannot be resolved. What am I doing wrong? Have I imported the correct jars?
package Input;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.concurrent.Future;
import org.xml.sax.SAXException;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
public class TBB {
public static FileInputStream convert(InputStream docxInputStream) throws FileNotFoundException {
FileInputStream inputStream = null;
try (OutputStream outputStream = new FileOutputStream(new File("/Users/sebastianzeki/mydoc.docx"))) {
IConverter converter = LocalConverter.builder().build();
converter
.convert(docxInputStream).as(DocumentType.DOCX)
.to(outputStream).as(DocumentType.PDF)
.prioritizeWith(1000).schedule();
inputStream = new FileInputStream("/Users/sebastianzeki/mydoc.docx");
} catch (Exception e) {
e.getMessage();
}
return inputStream;
}
}
documents4j is not compatible with Mac. Look at your stacktrace and you will find something like: com.documents4j.throwables.ConverterAccessException: Unable to run script: /Applications/Tomcat-9.0.1/temp/1564683313105-0/word_start775650809.vbs Documents4j is running a generated vbScript under the hood. There is no way for mac to run vbScript as far as I know. I had to install a Windows vm with Word installed on my server and use the documents4j remote api to call into the windows vm to do the conversions.
I want to create a RapidMiner classifier on Java that classifies a user based on his/her touch input. I have searched for days now, I have RapidMiner Studio and have downloaded the rapidMiner-studio from github - but I don't know which one should I include on my library/build path.
Do I just include all of them? Any help would be greatly appreciated.
I found the answer to my question, so far I found out that you can run a process ( a .rmp) file you created from RapidMiner in Eclipse, and the JAR files i added are located at C:\Program Files\RapidMiner\RapidMiner Studio\lib
Here's my code:
package rapid_process;
import com.rapidminer.Process;
import com.rapidminer.RapidMiner;
import com.rapidminer.operator.Operator;
import com.rapidminer.operator.OperatorException;
//import com.rapidminer.operator.io.ExcelExampleSource;
import com.rapidminer.tools.XMLException;
import java.io.File;
import java.io.IOException;
import java.lang.Object;
public class process {
public static void main(String[] args) throws IOException, XMLException, OperatorException {
/*// Path to process-definition
final String processPath = "C:/Users/Evie/.RapidMiner/repositories/Local Repository/processes/Compare ROCs.rmp";
// Init RapidMiner
RapidMiner.setExecutionMode(ExecutionMode.COMMAND_LINE);
RapidMiner.init();
// Load process
final com.rapidminer.Process process = new com.rapidminer.Process(new File(processPath));
process.run();*/
try {
RapidMiner.setExecutionMode(RapidMiner.ExecutionMode.COMMAND_LINE);
RapidMiner.init();
Process process = new Process(new File("C:/Users/YourUser/.RapidMiner/repositories/Local Repository/processes/Compare ROCs.rmp"));
process.run();
} catch (IOException | XMLException | OperatorException ex) {
ex.printStackTrace();
}
}
}
I have a file
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.util.*;
public class ViewCount extends Configured implements Tool {
public static void main(String args[]) throws Exception {
int res = ToolRunner.run(new ViewCount(), args);
System.exit(res);
}
public int run(String[] args) throws Exception {
//Path inputPath = new Path(args[0]);
Path inputPath = Paths.get("C:/WorkSpace/input.txt");
Path outputPath = Paths.get("C:/WorkSpace/output.txt");
Configuration conf = getConf();
Job job = new Job(conf, this.getClass().toString());
I try to run a the app in windows. How can I set inputPath and outputPath? The method I use now doesn't work. Before I had
Path inputPath = new Path(args[0]);
Path outputPath = new Path(args[1]);
and I had to go to the command line. Now I want to run the app from the IDE.
I'm getting
Required:
org.apache.hadoop.fs.Path
Found:
java.nio.file.Path
For Eclipse, you could set arguments :
Run -> run configuration -> arguments.
It should be the same in Intellij.
The error tells you that it is expecting a org.apache.hadoop.fs.Path, but instead it receives a java.nio.file.Paths.
This means that you should change the second import of your code to
org.apache.hadoop.fs.Path. IDEs import suggestions can be wrong some times ;)
Change the import and then use the method that you already had to add the input and output path. Those arguments are given in Eclipse with right-clicking the project -> Run as -> Run configurations -> arguments. The two paths should be white-space separated. Apply and run!
For the next executions, just run the project.
I have images of codes that I want to decode. How can I use zxing so that I specify the image location and get the decoded text back, and in case the decoding fails (it will for some images, that's the project), it gives me an error.
How can I setup zxing on my Windows machine? I downloaded the jar file, but I don't know where to start. I understand I'll have to create a code to read the image and supply it to the library reader method, but a guide how to do that would be very helpful.
I was able to do it. Downloaded the source and added the following code. Bit rustic, but gets the work done.
import com.google.zxing.NotFoundException;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType;
import com.google.zxing.Reader;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.Result;
import com.google.zxing.LuminanceSource;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.*;
import com.google.zxing.qrcode.QRCodeReader;
class qr
{
public static void main(String args[])
{
Reader xReader = new QRCodeReader();
BufferedImage dest = null;
try
{
dest = ImageIO.read(new File(args[0]));
}
catch(IOException e)
{
System.out.println("Cannot load input image");
}
LuminanceSource source = new BufferedImageLuminanceSource(dest);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Vector<BarcodeFormat> barcodeFormats = new Vector<BarcodeFormat>();
barcodeFormats.add(BarcodeFormat.QR_CODE);
HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>(3);
decodeHints.put(DecodeHintType.POSSIBLE_FORMATS, barcodeFormats);
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
Result result = null;
try
{
result = xReader.decode(bitmap, decodeHints);
System.out.println("Code Decoded");
String text = result.getText();
System.out.println(text);
}
catch(NotFoundException e)
{
System.out.println("Decoding Failed");
}
catch(ChecksumException e)
{
System.out.println("Checksum error");
}
catch(FormatException e)
{
System.out.println("Wrong format");
}
}
}
The project includes a class called CommandLineRunner which you can simply call from the command line. You can also look at its source to see how it works and reuse it.
There is nothing to install or set up. It's a library. Typically you don't download the jar but declare it as a dependency in your Maven-based project.
If you just want to send an image to decode, use http://zxing.org/w/decode.jspx