Access Java class defined in a file with many classes from Kotlin - java

I have a package which contains a public Kotlin class and a Java file with many package-private top-level classes like so:
com.example.mypackage
- KotlinClass.kt
- JavaClasses.java
-- Class1
-- Class2
-- ...
If I try to access any class from JavaClasses in KotlinClass I get Unresolved reference error. Is it possible to access these classes?
I cannot change JavaClasses.java because it's generated.

It should be possible to access these classes with the setup that you described, the only time I get a an Unresolved reference error is when there are errors within the java file. Perhaps because JavaClasses.java is generated it doesn't have the correct package declaration at the top of the file, which in your case should be package com.example.mypackage;

Related

compiling java package classes. Cannot access Class. class file contains wrong class while working with packages

I was trying to compile a few files in a package in java. The package name is library. Please have a look at the following details.
This is my Directory Structure:
javalearning
---library
------ParentClass.java
------ChildClass.java
I tried to compile in the following way:
current directory: javalearning
javac library/ParentClass.java //this compilation works fine
javac library/ChildClass.java //error over here
The following is the ParentClass.java:
package library;
class Parentclass{
...
}
The following is the ChildClass.java:
package library;
class ChildClass extends ParentClass{
...
}
The error is as follows:
cannot access ParentClass
bad class file: .\library\ParentClass.class
Please remove or make sure it appears in the correct sub directory of the classpath
You've got a casing issue:
class Parentclass
That's not the same as the filename ParentClass.class, nor is it the same as the class you're trying to use in ChildClass: class ChildClass extends ParentClass.
Java classnames are case-sensitive, but Windows filenames aren't. If the class had been public, the compiler would have validated that the names matched - but for non-public classes, there's no requirement for that.
The fact that you've ended up with ParentClass.class suggests that at some point it was declared as ParentClass, but then you changed the declared name and when recompiling, Windows just overwrote the content of the current file rather than effectively creating Parentclass.class.
Make sure your declared class name exactly matches the filename. You may well want to delete all your class files before recompiling, just to get out of a confusing state.

JMeter Beanshell share class definitions

is there a way to share class definitions between scripts created purely in jmeter? For instance if I had the following structure:
Thread1
-BSSampler
-BSSample2
How can I create a class in BSSampler and use that same class definition in BSSample2 explicitly? Or would I have to push the class definition out to a file and use
${__beanShell(source("filename.bsh"))}
to share the same class definitions? Right now it's saying it doesn't recognize the class definition because it's a different namespace.
You can declare your classes and functions in a bsh file that you reference in user.properties through:
beanshell.server.file=../extras/startup.bsh
You can have a look at this file in extras/startup.bsh

jaxb.index and nested classes (and OSGi)

When I try to refer to nested classes within my jaxb.index file, an exception is thrown during serialization. How can this be avoided?
This is in an Eclipse RCP application. The classes causing the exception are in a different plug-in than the one that creates the JAXB context and initiates serialization. The classes are in one of the plug-in's exported packages.
The class structure looks like this (names have been changed):
#XmlRootElement(name="foo")
#XmlAccessorType (XmlAccessType.FIELD)
public class Foo extends AbstractFoo {
...
#XmlRootElement(name="fooMetric")
#XmlAccessorType (XmlAccessType.FIELD)
public static class FooMetric implements IFooMetric {
...
}
}
The jaxb.index file contains these:
Foo
Foo.FooMetric
During serialization, the exception says to use "OuterClass.InnerClass" -- which I'm doing.
javax.xml.bind.JAXBException: error loading class "Foo.FooMetric" listed in com/mypackage/jaxb.index, make sure that entries are accessable on CLASSPATH and of the form "ClassName" or "OuterClass.InnerClass", not "ClassName.class" or "fully.qualified.ClassName"
- with linked exception:
[java.lang.ClassNotFoundException: com.mypackage.Foo.FooMetric]
The javadocs ("Format for jaxb.index") also suggests that jaxb.index can contain entries of the form OuterClass.InnerClass.
Constraints on class name occuring in a jaxb.index file are:
Must not end with ".class".
Class names are resolved relative to package containing jaxb.index file. Only classes occuring directly in package containing jaxb.index file are allowed.
Fully qualified class names are not allowed. A qualified class name,relative to current package, is only allowed to specify a nested or inner class.
However, this does not appear to work. What will make it work?
The solution I found (by trial and error) was to use OuterClass$InnerClass in jaxb.index instead of OuterClass.InnerClass. This allows serialization to complete successfully.
However, I haven't found any authoritative source that recommends this.
[I'm posting this solution per stackoverflow guidelines, but would love to see and accept a better answer.]

Java Adding External Library failed when Self-defined Package Exists

Recently I have encountered some problem which seems a little strange to me.
In order to use some predefined class, I imported two .jar files say foo.jar and bar.jar(Both were written by others)
And my source code is like the following:
package jerry.deque
public class Deque {
.....
.....
Foo item = new Foo(); //Already defined in the foo.jar
.....
}
I added the external library exactly as what How to Import a Jar in Eclipse
did. But when I tried to use the class defined in foo.jar Eclipse shows me that "Foo can't be resolved to a type".
I spent a lot of time to fix this problem and finally succeeded after I removed
the clause: "package jerry.deque" at the beginning of my class file.
I think this is weird because just a few days ago when I was doing some Android development, I followed the same way to add a Twitter API library. And it works fine even when I declared "package jerry.search_twittes" at the beginning of my .java
file. I'm confused by this problem and couldn't figure out what's going wrong. Could someone help me to explain it in detail? Thanks very much.
Check that Foo is same package as Deque class. If they are not same
package, you need to import Foo class in Deque class.
For example,
package jerry.deque;
import packagename.foo; // packagename.foo
public class Deque {
.....
.....
Foo item = new Foo(); //Already defined in the foo.jar
.....
}
Added Explanation
I want you to check access modifier of Foo class carefully.
There are 2 access level for top level(class) access control . These
are public, or package-private (no explicit modifier).
Your Foo class is under default package(not specified package)and may be no explicit access modifier. Hope so! Then, all classes under default package can access to Foo class. That's why when you remove package jerry.deque clause, it works.
Similarly, I want you to check Android development java code in which it works fine even when you declared "package jerry.search_twittes". In that case, classes inside Twitter API library's access modifier is public.So you can access it from anywhere.
For more information you can read this.Is this information helpful???
Foo is in default package. Classes from default package cannot be imported directly.
So when you remove the package declaration in your code, you don't get the error.
You can look for reflection api or write a proxy in the default package for that class.

How to mention the class path

How can I mention the path of a class as in following code?
Class cl=Class.forName("SomeClass");
This works well if the "SomeClass" is located in the same directory of the calling class. But how to check for a class from a different directory, I saw the syntax for that is like xxxx.yyyy.class, but could not make out what those 'x's and'y's stand for. please help.
Thanks in advance.
Use the fully-qualified class name. For instance, to do this with the Java SE String class, which is in the java.lang package:
Class clazz = Class.forName("java.lang.String");
Those xxx and yyy stands for package names. Packages are normally represented by directories on disk with the same name as the package. When you create a class file you can specify which package the class goes by stating package xxx.yyy at the top of the file.
Referring to "SomeClass" without a package name will try to load a class named SomeClass in the default package.
Use Class.forName although make sure you deal with a possible ClassNotFoundException exception. This is a runtime exception so it does not mean you need to catch it. This is how you will know if you path to the class is correct. The problem is that if it cannot find the class funny things can happen with your code.
Class.forName(com.my.package.ClassName)

Categories

Resources