Bad class file when calling in another java file - java

When declaring a java file in another java file I get the following error.
The java file which is causing the error has the following code in
public class NumberSettingsFile
{
int maxSortBoxes = 50;
public int getMaxSortBoxes()
{
return maxSortBoxes;
}
}
And I am declaring it using the following code
NumberSettingsFile uniSet = new NumberSettingsFile();
Would someone please be able to explain what I am doing wrong?

The problem was caused by not declaring the package in which the java file was in. The code should have read
package GUIMain;
public class NumberSettingsFile
{
int maxSortBoxes = 50;
public int getMaxSortBoxes()
{
return maxSortBoxes;
}
}

Related

Exception in thread "main" java.lang.UnsatisfiedLinkError:proiectP.JavatoC.getval(I)I

This is my code and i have added the .dll to the place where Java_Home is. And i have this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: proiectP.JavatoC.getval(I)I at proiectP.JavatoC.getval(Native
Method) at proiectP.JavatoC.main(JavatoC.java:19)
public class JavatoC {
public native int getval(int b);
static {
System.loadLibrary("main");
}
public static void main(String[] args) {
try {
int a;
int b=3;
a= new JavatoC().getval(b);
System.out.println(a);
} catch(Exception e) {
System.out.println(e);
}
}
}
I also tried to write a= new proiectP.JavatoC().getval(b) as proiectP is the package. It doesn't work.
It seems library link not done right. There is a problem in the System.loadLibrary. Because it can't show the required method.
These are requirements for using a native code in Java, And I don't know which has not been observed :
First, make sure that the native file is correct, the getval method must be in that dll, exactly with the same specifications of name, input and output.
Second, it is exactly compiled for this use.
The last is in the right direction with the right name.

Finding/Resolving dependent DLLs of a DLL using Java

I want to figure out which dependent DLLs are missing when loading a DLL fails. By loading a DLL using Java's System#loadLibrary, I only get a long negative exit code such as -1073741515 instead of a Windows error message stating which DLL is missing. This is unhelpful for addressing the problem.
My idea was to parse dependent DLLs from the DLL using Java and loading them one by one to figure out which one throws an UnsatisfiedLinkError. I found a library called pecoff4j which claims to parse PE executables but I'm not able to parse the imported DLL names:
PE pe = PEParser.parse("C:\\Users\\User\\Desktop\\my.dll");
final ImportDirectory importTable = pe.getImageData().getImportTable();
for (int i = 0; i < importTable.size(); i++)
{
String name = importTable.getName(i);
System.out.println(name);
}
This yields the following exception since the names don't seem to be available:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at org.boris.pecoff4j.ImportDirectory.getName(ImportDirectory.java:39)
ImportDirectory.java:
package org.boris.pecoff4j;
import org.boris.pecoff4j.util.DataObject;
import java.util.ArrayList;
import java.util.List;
public class ImportDirectory extends DataObject {
private List<ImportDirectoryEntry> entries = new ArrayList();
private List<String> names = new ArrayList();
private List<ImportDirectoryTable> nameTables = new ArrayList();
private List<ImportDirectoryTable> addressTables = new ArrayList();
public void add(ImportDirectoryEntry entry) {
entries.add(entry);
}
public void add(String name, ImportDirectoryTable names,
ImportDirectoryTable addresses) {
this.names.add(name);
nameTables.add(names);
addressTables.add(addresses);
}
public int size() {
return entries.size();
}
public String getName(int index) {
return names.get(index);
}
public ImportDirectoryTable getNameTable(int index) {
return nameTables.get(index);
}
public ImportDirectoryTable getAddressTable(int index) {
return addressTables.get(index);
}
public ImportDirectoryEntry getEntry(int index) {
return entries.get(index);
}
}
Visual Studio's dumpbin works but I need a Java based solution to integrate into an application which is distributed to users who don't necessarily have Visual Studio installed.
If you think there is a better/simpler way to figure out or prevent missing DLLs when loading a DLL using Java, feel free to let me know as well.

Calling Java Code in C# using IKVM results in System.TypeInitializationException error

I am currently trying to call Java Code in C#. One possibility is IKVM, whereupon I looked at a tutorial for this tool. I have to say, and that's really curious: the tool seems to work in part.
But now to my problem:
So I took the following tutorial (https://www.codeproject.com/Articles/594632/IKVM-NET-in-Details). Following this example, I wrote my own Java code. In addition, I have added a few more methods to the java file. My source code for testing is relatively short:
The Java source code:
package TestProject;
public class TestClassJava {
public static void Print() {
System.out.println("Hi C# from JAVA");
}
public static void PrintStr(String str) {
System.out.println(str);
}
public static String returnString() {
return "Hi C# from Java method";
}
public static String returnInputString(String input) {
return input;
}
public static int retInt() {
return 42;
}
public static int returnIntNumber(int inp) {
return inp;
}
public static boolean returnTrueBoolean() {
return true;
}
}
The C# source code:
using System;
using System.IO;
using TestProject;
using ikvm.io;
using ikvm.lang;
using ikvm;
using ikvm.runtime;
using ikvm.extensions;
namespace IKVM_Test_Case_08_08_2019
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(TestClassJava.retInt()); // shows: 42 (works!)
Console.WriteLine(TestClassJava.returnString()); // shows: Hi C# from Java method (works!)
TestClassJava.Print(); // here appears the error System.TypeInitializationException
TestClassJava.PrintStr("Hallo"); // here appears the error System.TypeInitializationException
Console.WriteLine(TestClassJava.Print()); // can not convert from void to bool
}
}
}
The whimsical part now happens while running the program in C#. I try the methods in C# via Console.WriteLine(TestClassJava.retInt()); then, for example, the number 42 will be given to me, as it should be. I can also call the method returnString().
In the methods without return value, however, Print() & PrintStr(String str), I always get the following error message:
Error message:
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'java.lang.StdIO' threw an exception.
Source=IKVM.OpenJDK.Core
StackTrace:
at java.lang.System.get_out()
at TestProject.TestClassJava.Print()
at IKVM_Test_Case_08_08_2019.Program.Main(String[] args) in C:\Users\...\source\repos\IKVM_Test_Case_08_08_2019\IKVM_Test_Case_08_08_2019\Program.cs:line 19
Inner Exception 1:
MissingMethodException: Method not found: 'Void System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.Security.AccessControl.FileSystemRights, System.IO.FileShare, Int32, System.IO.FileOptions)'.
I can not quite explain that, so I asked this question in the hope, that someone knows an advice.
According to the quoted tutorial, it all had to work that way. Nevertheless, I get this error message.
I hope my question is so far understandable.

Retrieve code executed by function in Java

I'm trying to analyse some bits of Java-code, looking if the code is written too complexly. I start with a String containing the contents of a Java-class.
From there I want to retrieve, given a function-name, the "inner code" by that function. In this example:
public class testClass{
public int testFunction(char x) throws Exception{
if(x=='a'){
return 1;
}else if(x=='{'){
return 2;
}else{
return 3;
}
}
public int testFunctionTwo(int y){
return y;
}
}
I want to get, when I call String code = getcode("testFunction");, that code contains if(x=='a'){ ... return 3; }. I've made the input code extra ugly, to demonstrate some of the problems one might encounter when doing character-by-character-analysis (because of the else if, the curly brackets will no longer match, because of the Exception thrown, the function declaration is not of the form functionName{ //contents }, etc.)
Is there a solid way to get the contents of testFunction, or should I implement all problems described manually?
You need to a java parser. I worked too with QDox. it is easy to use. example here:
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;
import java.io.File;
import java.io.IOException;
public class Parser {
public void parseFile() throws IOException {
File file = new File("/path/to/testClass.java");
JavaProjectBuilder builder = new JavaProjectBuilder();
builder.addSource(file);
for (JavaClass javaClass : builder.getClasses()) {
if (javaClass.getName().equals("testClass")) {
for (JavaMethod javaMethod : javaClass.getMethods()) {
if (javaMethod.getName().equals("testMethod")) {
System.out.println(javaMethod.getSourceCode());
}
}
}
}
}
}
Have you considered using a parser to read your code? There are a lot of parsers out there, the last time I worked on a problem like this http://qdox.codehaus.org made short work of these kinds of problems.

java.lang.ClassNotFoundException when instantiating an inner class

I have a following problem with an innner class. Here's the code:
public class PGZUserManagerBean {
// joomla login as separate thread
private class JoomlaLogin extends Thread {
private AuthJoomla authJoomla;
public JoomlaLogin(AuthJoomla authJoomla){
this.authJoomla = authJoomla;
}
#Override
public void run(){
this.authJoomla.authJoomla();
}
}
public void validateuser(){
AuthJoomla authJoomla = new AuthJoomla();
JoomlaLogin joomlaLogin = new JoomlaLogin(authJoomla);
joomlaLogin.start();
}
}
I'm getting java.lang.ClassNotFoundException: PGZUserManagerBean$JoomlaLogin on runtime. I'm using Java 1.6.
Thank you for the help in advance.
al
I strongly suspect that you've copied the class files from one place to another (or put them in a jar file) but you've failed to copy/include PGZUserManagerBean$JoomlaLogin.class.
Check where you're running the code, and look for the class file that the JVM can't find. It will definitely be in your compilation output.

Categories