nikcho-kouhai
nikcho-kouhai
JCHJava Community | Help. Code. Learn.
Created by nikcho-kouhai on 8/4/2024 in #java-help
Weird class cast behavior
So I have this code:
class SearcherAllocator extends SearcherBase.Pointer_Alloc{
@Override
public Pointer call(){

class JSearcher extends Searcher{

}

return new JSearcher();

}
}
Searcher.alloc(new SearcherAllocator().retainReference());

Searcher searcher = (Searcher) Searcher.alloc().call();
class SearcherAllocator extends SearcherBase.Pointer_Alloc{
@Override
public Pointer call(){

class JSearcher extends Searcher{

}

return new JSearcher();

}
}
Searcher.alloc(new SearcherAllocator().retainReference());

Searcher searcher = (Searcher) Searcher.alloc().call();
May look a bit messy but essentially the class hierarchy of Searcher is Searcher extends SearcherBase extends Pointer. However, even though Searcher extends Pointer, when the line
Searcher searcher = (Searcher) Searcher.alloc().call();
Searcher searcher = (Searcher) Searcher.alloc().call();
runs, it gives me this: Exception in thread "main" java.lang.ClassCastException: class org.bytedeco.javacpp.Pointer cannot be cast to class com.niki.nativeplayer.Bridge.Searcher (org.bytedeco.javacpp.Pointer and com.niki.nativeplayer.Bridge.Searcher are in unnamed module of loader 'app') there are two Searcher.alloc() functions, one is a setter and one is a getter. I think that part is self explanatory. I tried to replicate this error with this:
public static void main(String[] args) {
class Pointer{


}
class SearcherBase extends Pointer{


}
class Searcher extends SearcherBase{

}

Supplier<Pointer> allocator =()->{
class JSearcher extends Searcher{

}
return new JSearcher();
};
Searcher searcher= (Searcher) allocator.get();
}
public static void main(String[] args) {
class Pointer{


}
class SearcherBase extends Pointer{


}
class Searcher extends SearcherBase{

}

Supplier<Pointer> allocator =()->{
class JSearcher extends Searcher{

}
return new JSearcher();
};
Searcher searcher= (Searcher) allocator.get();
}
But this code ,which should be pretty much equivalent, doesn't throw that exception.
61 replies
JCHJava Community | Help. Code. Learn.
Created by nikcho-kouhai on 4/5/2024 in #java-help
maven-plugin-plugin doesn't package plugin descriptor in jar file
I have this in my pom.xml file as a maven plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.9.0</version>
<configuration>
<packagingTypes>
<packagingType>jar</packagingType>
</packagingTypes>


</configuration>
<executions>
<execution>
<id>default-descriptor</id>
<phase>process-classes</phase>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.9.0</version>
<configuration>
<packagingTypes>
<packagingType>jar</packagingType>
</packagingTypes>


</configuration>
<executions>
<execution>
<id>default-descriptor</id>
<phase>process-classes</phase>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
The packaging of my project isn't currently set to
<packaging>maven-plugin</packaging>
<packaging>maven-plugin</packaging>
but even If I try to set it to that the plugin.xml still doesn't get packaged in the jar file. To be clear plugin.xml gets generated under classes/META-INF/maven , but when I open the generated .jar file and go to META-INF/maven the plugin.xml file isn't there. And so when I try to include this project as a plugin it fails with plugin descriptor not found.
45 replies
JCHJava Community | Help. Code. Learn.
Created by nikcho-kouhai on 2/20/2024 in #java-help
Concurrent ArrayList read/write
I have a situation where one thread puts data in an ArrayList and another thread reads that data. I am not really concerned whether the thread that reads the data can instantly tell that the data is there, but rather if there is a chance the reader thread might not ever see the data at all. Here is an example:
public class Main {


final static ArrayList<Integer> list= new ArrayList<>();

public static void main(String[] args) {
Thread writer = new Thread(Main::write);
Thread reader = new Thread(Main::read);
writer.start();
reader.start();
}
public static void write(){
while (true){
list.add(1);
}
}
public static void read(){
int currentIndex=0;
while (true){
if(currentIndex>=list.size()){
try {
Thread.sleep(1);
continue;
} catch (InterruptedException e) {
return;
}
}
int element = list.get(currentIndex);
currentIndex++;
//do stuff with element...
}
}
}
public class Main {


final static ArrayList<Integer> list= new ArrayList<>();

public static void main(String[] args) {
Thread writer = new Thread(Main::write);
Thread reader = new Thread(Main::read);
writer.start();
reader.start();
}
public static void write(){
while (true){
list.add(1);
}
}
public static void read(){
int currentIndex=0;
while (true){
if(currentIndex>=list.size()){
try {
Thread.sleep(1);
continue;
} catch (InterruptedException e) {
return;
}
}
int element = list.get(currentIndex);
currentIndex++;
//do stuff with element...
}
}
}
In this scenario if for some reason the writer thread is taking a while to add something to the list, is it possible the reader thread might do something like cache the entire list and pretty much never see when a new element is added so it just keeps sleeping? And if it is possible is there a way to prevent it without synchronizing access to the entire structure, because I really don't need to. I have been running my program like this for quite some time and haven't run into any issues, but this potential problem lingers in the back of my mind.
4 replies
JCHJava Community | Help. Code. Learn.
Created by nikcho-kouhai on 2/14/2024 in #java-help
Non-interruptible thread sleeping
Is is possible to sleep a thread in such a way that's not affected by the thread being interrupted? I know when a thread is waiting to acquire a lock it's not bothered by interruptions so is there such a thing without having to sit on some objects monitor?
18 replies
JCHJava Community | Help. Code. Learn.
Created by nikcho-kouhai on 5/30/2023 in #java-help
MediaPlayer question
So I am trying to make something of a video player and I am using MediaPlayer and a temporary .mp4 file to achieve that. Basically there is another thread other than the UI thread that has an OutputStream writing to the file (No need to question that part I know it sounds weird but the part where the outputstream is writing to the file works.) Now when I try to use the seek() method of the MediaPlayer and go to the time where I know for sure there is data the MediaPlayer stops. When I check its status it says PLAYING and if I skip around that area it usually starts playing as normal again. This whole thing probably sounds a bit convoluted but I have my reasons and I am wondering if this is because of the MediaPlayer class (perhaps because of some buffering behaviour) or something else and if it is because of the MediaPlayer are there alternatives I can use(maybe ones that directly take in a stream or ones that don't have the issue that's causing this whole mess). If there is anything I need to clarify so I can help you help me I will try to the best of my ability.
4 replies
JCHJava Community | Help. Code. Learn.
Created by nikcho-kouhai on 5/28/2023 in #java-help
Synchronizing threads across different modules
So I have two modules let's call them m1 and m2. So m1 depends on m2 and m1 starts a Thread that does some things using m2, the problem is that I need the thread in m1 and the thread in m2 to exchange information from time to time and for that I would need m2 to also depend on m1 but that creates a circular dependency and as far as I'm aware that's bad (correct me if I am wrong). How should I avoid this? Should I make another module which m1 and m2 depend on and that module handles synchronization and and everything across m1 and m2 or does that just lead to the same problem with extra steps?
71 replies
JCHJava Community | Help. Code. Learn.
Created by nikcho-kouhai on 5/18/2023 in #java-help
Error occurred during initialization of boot layerjava.lang.module.ResolutionException:
So I tried to use one java project I have as a library for another project. I built the project into a .jar file and put it in as a library in the other project. In the module-info.java for the project I am trying to import as a library I have a dependency for fastjson. Here are the contents of module-info.java:
module java.downloader{
requires fastjson;
exports com.help.downloader;

}
module java.downloader{
requires fastjson;
exports com.help.downloader;

}
here is the module-info.java for the module I am trying to import in:
module com.example.attempt {
requires javafx.controls;
requires javafx.fxml;
requires javafx.media;
requires java.downloader;


opens com.example.attempt to javafx.fxml;
exports com.example.attempt;

}
module com.example.attempt {
requires javafx.controls;
requires javafx.fxml;
requires javafx.media;
requires java.downloader;


opens com.example.attempt to javafx.fxml;
exports com.example.attempt;

}
when I try to run it I get this: Error occurred during initialization of boot layer java.lang.module.ResolutionException: Module java.downloader contains package com.alibaba.fastjson.util, module fastjson exports package com.alibaba.fastjson.util to java.downloader
86 replies
JCHJava Community | Help. Code. Learn.
Created by nikcho-kouhai on 5/17/2023 in #java-help
module-info.java treated as package-info.java
There is not much more to this problem the title pretty much explains it. I have made the module-info.java file in src/main/java and it is being recognized as a package-info.java which is not what I want. Am I doing something wrong?
32 replies
JCHJava Community | Help. Code. Learn.
Created by nikcho-kouhai on 5/7/2023 in #java-help
Maven dependencies
Hello! I would like to ask how I can find out what the groupId, artifactId and version I need to put in my maven dependency when I am looking for a particular dependency. Like sometimes I am looking for an answer to some question and stumble upon a stackoverflow post which uses some dependency from apache for example. when I go to the apache documentation for the class that is used there isn't really anything that indicates what I need to put in my pom.xml file. I could be wrong about this or just plain blind but this has happened too many times now and I usually resolve it with chatGPT but its starting to get quite annoying. For the sake of giving a particular example, say I want to use IOUtils from apache's org.apache.commons.io package. How do I add the dependency for that to my pom.xml?
10 replies