Here is what my code looks like:
public class ExcelManip {
public static void main(String args[])
throws ExcelException, FileNotFoundException {
Application application = new Application();
File xslFile =
new File("C:\\Users\\Nathan.Shnipes\\Documents\\Workbook.xlsx");
Workbook workbook = application.openWorkbook(xslFile, true);
Worksheet worksheet = workbook.getWorksheet(1);
Range range = worksheet.getRange("A1:AD1");
Variant[][] results = range.getValues();
Cell cell = worksheet.getCell("A1");
System.out.println(cell.getString());
System.out.println(results[0][0].getValue());
System.out.println(
"C:\\Users\\Nathan.Shnipes\\Documents\\Workbook.xlsx");
Range range2 = worksheet.getRange("o1:o500");
Variant[][] dates = range2.getValues();
System.out.println(dates[0][0].getValue());
}
}
or as an image:
It's been throwing an error consistently on line 35. I deleted everything past line 35 as it worked up to that point, but it didn't help. I really have no idea whats wrong. I have tried copying the body of the program and placing it into a new class, but for whatever reason that has been throwing a no main error, even though it had the line public static void main(String[] args) throws ***
You have a red exclamaition mark on your project. Eclipse is likly to not build your project if build path problems (e.g.) aren't resolved. So go to the Problems tab and try to resolve your errors there. If that is finished Eclipse will build your project and gives more helpful errors
Edit
and that is why you are running the old version (no new build replaced your old one), in which there is actually a line 35.
Related
I want to read pdf table, right now I'am using PDFxStream to get table data from PDF, but when I encounter Japanese characters it becomes strange characters like this "ዊᮻᏒⒷⓄ䋳ৼ⋡䋱䋱⇟䋲ภ" and it has warning because there is an exception.
this is the warning:
WARNING: Could not parse content stream of object 9,0 due to exception {java.lang.IllegalArgumentException: IV buffer too short for given offset/length combination} (3food.pdf)
java.lang.IllegalArgumentException: IV buffer too short for given offset/length combination
this is mycode :
public class Sample {
public static void main (String[] args) throws java.io.IOException {
//String pdfFilePath = args[0];
System.setProperty("pdfxs.config.property", "N");
Document pdf = PDF.open("3food.pdf");
StringBuilder text = new StringBuilder();
Page wantedPage = pdf.getPage(3);
wantedPage.pipe(new OutputTarget(text));
pdf.close();
System.out.println(text);
}
}
If you have any clue what happen, I will really appreciate it, Thank you in advance
This is actually a bug that has been fixed in PDFxStream version 3.7.0, available here for download or inclusion in your Maven build:
https://www.snowtide.com/downloads
And as a side note, if you have any further issues with or questions about PDFxStream, I would urge you to get in touch with us directly # https://www.snowtide.com/contact 😄
I have a code in Java that opens a excel template by aspose library (it runs perfectly):
import com.aspose.cells.*;
import java.io.*;
public class test
{
public static void main(String[] args) throws Exception
{
System.setProperty("java.awt.headless", "true");
FileInputStream fstream = new FileInputStream("/home/vmlellis/Testes/aspose-cells/template.xlsx");
Workbook workbook = new Workbook(fstream);
workbook.save("final.xlsx");
}
}
After I run this on Ruby with RJB (Ruby Java Bridge):
require 'rjb'
#RJM Loading
JARS = Dir.glob('./jars/*.jar').join(':')
print JARS
Rjb::load(JARS, ['-Xmx512M'])
system = Rjb::import('java.lang.System')
file_input = Rjb::import('java.io.File')
file_input_stream = Rjb::import('java.io.FileInputStream')
workbook = Rjb::import('com.aspose.cells.Workbook')
system.setProperty("java.awt.headless", "true")
file_path = "/home/vmlellis/Testes/aspose-cells/template.xlsx"
file = file_input.new(file_path)
fin = file_input_stream.new(file)
wb = workbook.new(fin)
I get this error:
test.rb:57:in `new': Can't find file: java.io.FileInputStream#693a317a. (FileNotFoundException)
from aspose-test.rb:57:in `<main>'
Why? I run the same code... but in Ruby is not working! How do I fix this?
Update:
In documentation there is the the initializer: Workbook(java.io.InputStreamstream)... but it's not working in RJB. (How is this possible?)
Your program should have worked, but I could not find any reason why it didn't and I am looking into it.
Now the alternate approaches.
Approach 1
Use Workbook(String) constructor instead of Workbook(FileInputStream). This worked flawlessly at my end. The sample code is
require 'rjb'
#RJM Loading
JARS = Dir.glob('/home/saqib/cellslib/*.jar').join(':')
print JARS
Rjb::load(JARS, ['-Xmx512M'])
system = Rjb::import('java.lang.System')
workbook = Rjb::import('com.aspose.cells.Workbook')
system.setProperty("java.awt.headless", "true")
file_path = "/home/saqib/rjb/template.xlsx"
save_path = "/home/saqib/rjb/final.xlsx"
wb = workbook.new(file_path)
wb.save(save_path)
Approach 2
Write a new Java class library. Write all your Aspose.Cells related code in it. Expose very simple and basic methods that needs to be called from Ruby (RJB).
Why?
It is easy to write program in native Java language. If you use RJB, you need to perform a lot of code conversions
It is easy to debug and test in Java.
Usage of RJB will only be limited to calling methods from your own Java library. The RJB code will be small and basic.
Similar Example using own library
Create a new Java project, lets say "cellstest". Add a new public class in it.
package cellstest;
import com.aspose.cells.Workbook;
public class AsposeCellsUtil
{
public String doSomeOpOnWorkbook(String inFile, String outFile)
{
String result = "";
try
{
// Load the workbook
Workbook wb = new Workbook(inFile);
// Do some operation with this workbook
// ..................
// Save the workbook
wb.save(outFile);
// everything ok.
result = "ok";
}
catch(Exception ex)
{
// Return the exception to calling program
result = ex.toString();
}
return result;
}
}
Like this, add as many methods as you like, for each operation.
Build the project and copy the "cellstest.jar" in same folder where you copied Aspose.Cells jar files. You can return a String from your methods and check the return value in Ruby program for success or error code. The Ruby program will now be like
require 'rjb'
#RJM Loading
JARS = Dir.glob('/home/saqib/cellslib/*.jar').join(':')
print JARS
Rjb::load(JARS, ['-Xmx512M'])
system = Rjb::import('java.lang.System')
AsposeCellsUtil = Rjb::import('cellstest.AsposeCellsUtil')
system.setProperty("java.awt.headless", "true")
file_path = "/home/saqib/rjb/template.xlsx"
save_path = "/home/saqib/rjb/final.xlsx"
# initialize instance
asposeCellsUtil = AsposeCellsUtil.new()
# call methods
result = asposeCellsUtil.doSomeOpOnWorkbook(file_path, save_path)
puts result
PS. I work for Aspose as Developer Evangelist.
In your Java code, you pass a file name string into FileInputStream() constructor:
FileInputStream fstream = new FileInputStream("/home/vmlellis/Testes/aspose-cells/template.xlsx");
In your Ruby code, you pass a file object:
file = file_input.new(file_path)
fin = file_input_stream.new(file)
Have you tried to do the same thing as in Java?
fin = file_input_stream.new(file_path)
I am trying to use jaudiotagger for retriving id3 tags from mp3 file for my recent side project but I have run into this problem..I can't run the program because I get this error saying "MP3AudioHeader can not be resolved"..but I have imported everything that is needed as you can see...any suggestions would be helpful and here is the code I copied from the website
import org.jaudiotagger.*;
public class mainClass
{
public static void main(String[] args)
{
// the file we are going to read
File oSourceFile = new File("/Users/tushar_chutani/Music/iTunes/iTunes Media/Music/Fun_/We Are Young (feat. Janelle Monáe) - Single/01 We Are Young (feat. Janelle Monáe).mp3");
MP3File f = (Mp3File)AudioFileIO.read(oSourceFile);
MP3AudioHeader audioHeader = f.getAudioHeader();
audioHeader.getTrackLength();
audioHeader.getSampleRateAsNumber();
mp3AudioHeader.getChannels();
mp3AudioHeader.isVariableBitRate();
}
Your code is correct ,the mistake is that ,its not (Mp3File) its (MP3File) 'p' should be in capital letter. so correct is:-
MP3File f = (MP3File)AudioFileIO.read(oSourceFile);
i am a barely new to the java (was always on c# before) and need to create a swing application where i need to read a data from xls file. So i use jXL.
I have a class, which returns name of a first sheet from excel file, choosen in jFileChooser. Here is the code:
import java.io.File;
import jxl.Sheet;
import jxl.Workbook;
public class ExcelObject
{
private String filename = null;
private Workbook wb = null;
private Sheet sheet = null;
public ExcelObject(String f)
{
filename = f;
}
public String getSheetName()
{
String sheet_name = null;
try
{
wb = Workbook.getWorkbook(new File(filename));
sheet = wb.getSheet(0);
sheet_name = sheet.getName();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
wb.close();
}
return sheet_name;
}
}
in the program call looks like :
ExcelObject ex = new ExcelObject(filename);
String s = ex.getSheetName();
lblReport.setText(s);
So the issue is : when ran in the eclipse (3.4.2) i am getting a correct value, when jar is compiled, NO VALUE IS RETURNED! I mean lblReport is empty, with no exceptions and warnings.
Keep in mind : all other external jars work fine.
I tried a lot of things but none is working.
Also, if i do something like
ExcelObject ex = new ExcelObject(filename);
String s = ex.getSheetName();
// lblReportRun.setText(s);
lblReportRun.setText("Test");
lblAnyOtherLabel.setText("Test");
no text is displayed in the labels either, in compiled jar, and fine in eclipse.
This is probably because if some exception occurs when opening the workbook, you catch it, print the stack trace, and then close the workbook in the finally block. But since an exception occurred when calling Workbook.getWorkbook, the wb variable is still null, and you're trying to invoke close on it. So a NullPointerException happens while in the finally block.
Watch your console, you must have some exception popping up.
Also, note that fileName should be the only instance variable of your class, that Java variable normally don't contain underscores and use camelCase, and thet the fileName variable should be of type File, rather than String : you get a File from the file chooser, transform it into a string, and then transform it back to a File.
Another possibility, since it works in Eclipse and not when run externally, is that you forgot to put the jXL jar in the classpath, which causes some ClassNotFoundException when trying to use the ExcelObject class.
I have written a program in Java. But when I compile this program it shows an error
How to solve this problem?
the source code of the program is:
import org.rosuda.JRI.Rengine;
public class JavaGDExample1 {
public static void main(String[] args) {
Rengine re;
String[] dummyArgs = new String[1];
dummyArgs[0] = "--vanilla";
re = new Rengine(dummyArgs, false, null);
re.eval("library(JavaGD)");
// This is the critical line: Here, we tell R that the JavaGD() device that
// it is supposed to draw to is implemented in the class MyJavaGD. If it were
// in a package (say, my.package), this should be set to
// my/package/MyJavaGD1.
re.eval("Sys.putenv('JAVAGD_CLASS_NAME'='MyJavaGD1')");
re.eval("JavaGD()");
re.eval("plot(c(1,5,3,8,5), type='l', col=2)");
re.end();
}
}
it shows this error
No symbol for REngine
please reply
I assume that you are getting 'cannot find symbol' error message when you attempt to compile this. In that case, it means that you have not added the relevant JAR file into your classpath.
You can specify the classpath as follows.
java -cp <PATH_TO_YOUR_LIB> <YOUR_CLASSES>
If you are using an IDE, you will have to add these JAR files as libraries into it.