ZXing java.lang.NoClassDefFoundError: com/google/zxing/WriterException Error - java

Project Context
Currently trying to generate QR codes to implement some information within it, that includes data (such as a student ID number, first name, last name) and MAC address of local computer (testing on my desktop computer for now, but will be applied to individual raspberry Pis).
Error in Question
The Java Code
Ignore the data link input, this is for test purposes only.
public static void main(String[] args) throws Exception
{
MACFinder GM = new MACFinder();
GM.getMac();
System.out.println();
try
{
GM.generateQRCodeImage();
}
catch (WriterException e)
{
System.out.println("Couldn't generate QR code, WriterException...");
}
catch (IOException e)
{
System.out.println("Couldn't generate QR code, IOException...");
}
catch (NotFoundException e)
{
System.out.println("This library couldn't be found... (ZXing)");
}
}
public void generateQRCodeImage() throws WriterException, IOException, NotFoundException
{
String data = "https://www.youtube.com/watch?v=qzYpgbP8RHA";
String filepath = "C:\\Users\\JmJ23\\Documents\\Code\\Internship Code";
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, 250, 250);
Path path = FileSystems.getDefault().getPath(filepath);
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}
POM.xml Dependencies
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
I understand that a NoClassDefFoundError is when a library/class is called but the class in question is not found at runtime... but why would this be the case if I have installed it into my Maven project? As a note I cleaned the project multiple times but with no progress. Any advice?

Your build looks fine. You didn't say how you build and run this, but I expect you just tried to build a .jar and run that .jar, without building all of its dependencies into it. You need something like the Maven assembly plugin.

Related

Base64 image screenshot in extent report is showing garbage value on clicking the screenshot in the report

Base64 image in selenium extent report is not displayed correctly after clicking the image in the report.
Searched various sources to get the issue resolution but did not find the answer anywhere
//dependency in POM.xml Code:-
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.1.5</version>
</dependency>
//ExtentReporterListener Code :-
public synchronized void onTestFailure(ITestResult result) {
System.out.println((result.getMethod().getMethodName() + " failed!"));
test.get().fail("Test failed due to below error");
try {
test.get().fail(result.getThrowable(), MediaEntityBuilder.createScreenCaptureFromPath(TestUtil.getScreenshotExtent()).build());
} catch(IOException e) {
System.err.
println("Exception thrown while updating test fail status " + Arrays.toString(e.getStackTrace()));
}
test.get().getModel().setEndTime(getTime(result.getEndMillis()));
}
//Capture screenshots code:-
public class TestUtil {
public static String getScreenshotExtent() {
String Base64StringofScreenshot = "";
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
byte[] fileContent;
try {
fileContent = FileUtils.readFileToByteArray(src);
Base64StringofScreenshot = "data:image/png;base64," + Base64.getEncoder().encodeToString(fileContent);
} catch(IOException e) {
e.printStackTrace();
}
return Base64StringofScreenshot;
}
}
Can someone please help how to resolve this issue and correctly display the image in extent report after clicking it?
can you please let me know in which browser you are running this Automation suite?
for e.g: You are using Chrome browser in windows OS 64 bit, then you need to check the chromedriver.exe which you have downloaded, that is for 32 bit or 64 bit.If you are using the driver for 32 bit then this type of issue comes.
If you are still not finding any solution then let me know.

Slick2D/LWJGL crashes jvm when loading fonts (both TrueTypeFont and UnicodeFont)

OS: Windows 10
Dependencies:
<dependency>
<groupId>org.slick2d</groupId>
<artifactId>slick2d-core</artifactId>
<version>1.0.2</version>
</dependency>
LWJGL natives: 2.9.3
The code I'm using is:
public void init(GameContainer gc) throws SlickException {
try {
java.awt.Font temp = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, org.newdawn.slick.util.ResourceLoader.getResourceAsStream("resources/5x5_pixel.ttf"));
temp = temp.deriveFont(java.awt.Font.PLAIN, 5);
// Crashes here:
new TrueTypeFont(temp, false);
// and here:
new UnicodeFont(temp);
} catch (Exception e) {
e.printStackTrace();
}
It crashes without throwing an exception, so I'm unsure how to debug this. Everything renders/runs fine without the fonts, and I've tested multiple fonts all seem to crash the same way. (Note, it throws a different error if the font is missing, so I've looked at that too).
Thanks!

What am I doing wrong with my Dom4j Code?

Hey guys I am working on a load class for my project that loads and pulls a info from a xml files. I have been following a few guides online but I am running into the error java.lang.NoClassDefFoundError: org/jaxen/JaxenException. I know the code is finding the save file because I can print out the name. But when I try to pull out the info I get that error. Snippet of code is below if there is no error in there let me know and Ill post more.
public void LoadProjects()
{
try
{
Files.walk(Paths.get("D:/workspace/Project Program/Projects/")).forEach(filePath ->
{
if(Files.isRegularFile(filePath))
{
System.out.println("Testing");
try
{
SAXReader reader = new SAXReader();
Document document = reader.read(filePath.toFile());
System.out.println(document.getName());
Node node = document.selectSingleNode("///Project/Info");
//String name = node.valueOf("#Name");
//String projNum = node.valueOf("#ProjectNumber");
//node = document.selectSingleNode("//Project/Dates");
//String dueBy = node.valueOf("#DueBy");
//CButton temp = new CButton(name, projNum, dueBy);
//Console.console.AddToList(temp);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
catch(Exception e)
{
e.printStackTrace();
}
}
Did you add the jaxen jar to your servers lib?
Your code seems correct so far but please add as much information as possible from the beginning.
Ff you are using maven:
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>

Accessing a PDF in Jar

I'm creating a Java application using Netbeans. From the 'Help' Menu item, I'm required to open a PDF file. When I run the application via Netbeans, the document opens, but on opening via the jar file, it isn't opening. Is there anything that can be done?
m_aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Runtime rt = Runtime.getRuntime();
URL link2=getClass().getResource("/newpkg/Documentation.pdf");
String link=link2.toString();
link=link.substring(6);
System.out.println(link);
System.out.println(link2);
String link3="E:/new/build/classes/newpkg/Documentation.pdf";
try {
Process proc = rt.exec("rundll32.exe url.dll,FileProtocolHandler " + link3);
} catch (IOException ex) {
Logger.getLogger(Menubar1.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
The two outputs are as follows:
E:/new/build/classes/newpkg/Documentation.pdf
file:/E:/new/build/classes/newpkg/Documentation.pdf
Consider the above code snippet. On printing 'link',we can see that it is exactly same as the hard coded 'link3'. On using the hard coded 'link3' , the PDF file gets opened from jar application. But when we use link, though it is exactly same as 'link3', the PDF doesn't open.
This is most likely related to the incorrect PDF resource loading. In the IDE you have the PDF file either as part of the project structure or with a directly specified relative path. When a packaged application is running it does not see the resource.
EDIT:
Your code reveals the problem as I have described. The following method could be used to properly identify resource path.
public static URL getURL(final String pathAndFileName) {
return Thread.currentThread().getContextClassLoader().getResource(pathAndFileName);
}
Pls refer to this question, which might provide additional information.
Try out this:
m_aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
URL link2=Menubar1.class.getResource("/newpkg/Documentation.pdf");
String link=link2.toString();
link=link.substring(6);
System.out.println(link);
File file=new File(link);
System.out.println(file);
try {
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(Menubar1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});

How to find where the Startup folder is with Java?

In my Java program how can I find out where the Startup folder is on uers' PCs ? I know it's different on different versions of Windows. I only need my app to work on Windows. Any sample code ?
This should work:
System.getProperty("user.dir")
here you have an overviwe about system properties:
http://download.oracle.com/javase/tutorial/essential/environment/sysprop.html
With ShellLink creating shortcuts is very simple. https://github.com/BlackOverlord666/mslinks
Maven:
<dependency>
<groupId>com.github.vatbub</groupId>
<artifactId>mslinks</artifactId>
<version>1.0.5</version>
</dependency>
Refer answer by https://stackoverflow.com/a/38952899/4697928
private final String STARTUP_PATH = "/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup";
private void checkAutoStartPresent() {
File file = new File(System.getProperty("user.home") + STARTUP_PATH + "/YourShortcutFileName.lnk");
if (!file.exists()) { // shortcut not found
try {
ShellLink.createLink("/YourTargetProgramPath.exe", file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
}

Categories