Loading netbeans generated .jar's into matlab fails - java

I'm trying to get a "Hello World" like java project to be used in matlab. To start easy, I tried to make a simple netbeans project containing only 1 file with some basic functions to test.
With "HelloWorld.java" being:
public class HelloWorld{
public HelloWorld(){
setVersion(0);
}
public static void main(String[] args){
System.out.println("Hello World!");
}
public void setVersion(int aVersion){
if( aVersion < 0 ){
System.err.println("Improper version specified.");
}
else{
version = aVersion;
}
}
public int getVersion(){
return version;
}
private int version;
}
I built this in netbeans to create a .jar, but when trying to connect to it in matlab via the following code it cannot find HelloWorld when trying to make an object.
javaaddpath('C:\Users\<name>\Documents\Netbeans\HelloWorld\dist\HelloWorld.jar')
javaclasspath
myHelloObject = HelloWorld
It gives the following error
Undefined function or variable 'HelloWorld'.
Error in test (line 6)
myHelloObject = HelloWorld
However, when using "example jars" that I downloaded of the internet and connected them in matlab via the same method this seems to work, so I guess there is something wrong with the .jar generation.
What am I doing wrong? Thanks in advance.

Already found the answer: it had to do with Matlab using java 1.7 while the jar was generated in 1.8. Switching to another virtual machine (or generating the jar in another java version) solves the problems. https://nl.mathworks.com/matlabcentral/answers/130359-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-on-windows

Related

Error: IllegalAccessError Java, when using another library's command in a library

Here is my Java library (.aar) file's code.
package com.sahib.ffpy;
import com.arthenica.ffmpegkit.FFmpegSession;
import com.arthenica.ffmpegkit.FFmpegKit;
public class ffpy<command>
{
public static void Run(final String command) {
final FFmpegSession session = FFmpegKit.execute(command);
}
}
This is supposed to take a command and execute in FFmpegKit by ARTHENICA.
I am using this library with pyjnius to run it as I can not directly do.
FFmpegSession session = FFmpegKit.execute(command)
In python.
This is how my python code looks:
FFMPEG = autoclass('com.sahib.ffpy.ffpy')
FFMPEG.Run("-i "+INPUT_FILE+" -qscale:v "+str(FRAME_QUALITY)+" TEMP/frame%06d.jpg -hide_banner")
I am using Kivy/Buildozer.
Here are the Gradle dependencies in buildozer:
android.add_aars = ffpy-debug.aar
android.gradle_dependencies = "com.arthenica:ffmpeg-kit-full:5.1"
I have tried extracting the classes.jar from both ffmpegkit and this and combining them, which throws a different error (I know it's not the right way to do it). I tried building ffmpegkit myself. I have tried directly using FFmpeg Kit in Python, but I don't think it will work.

Java ImageIO resource path

I have a Java program that runs fine in Eclipse - when I export an executable jar file it gives me an exception.
I know it is because I am giving it the wrong path but I do not know how to fix. I tried using getClass().getResourceaAsStream() (or sth. like that) but that did not work as well.
The code I am using at the moment is the following:
line 31:
BufferedImage image = ImageIO.read(new File("/src/res/loading.png"));
Okay, so I created a simple project, made a directory called res and threw some images into it
Before anyone points out that this not Eclipse, it shouldn't matter, we've already established that the Jar file contains the res directory and images, it's about replicating the structure
I then wrote a really simple example...
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
System.out.println(Test.class.getResource("/res/0.jpg"));
System.out.println(getClass().getResource("/res/0.jpg"));
}
}
which outputs...
file:/.../Test/build/classes/res/0.jpg
file:/.../Test/build/classes/res/0.jpg
No, before any points out the IDE's environment is different from the command lines, I also ran it from the command line...
java -jar Test.jar
jar:file:/.../Test/dist/Test.jar!/res/0.jpg
jar:file:/.../Test/dist/Test.jar!/res/0.jpg

Interfacing with Java Functions in Haxe

I am trying to call an external Java function from Haxe using "extern".
Haxe Code :
extern class Ext
{
public static function test():String;
}
class Sample
{
public static function main()
{
trace(Ext.test());
}
}
Java Code :
public class Ext
{
public static String test()
{
return "Hello";
}
}
Both Sample.hx and Ext.java files are in the same folder.
When I try to execute haxe -main Sample -java Sample, I get the following error.
C:\Users\ila5\Desktop\CPP>haxe -main Sample -java Sample
haxelib run hxjava hxjava_build.txt --haxe-version 3201 --feature-level 1
javac.exe "-sourcepath" "src" "-d" "obj" "-g:none" "#cmd"
src\haxe\root\Sample.java:33: error: cannot find symbol
haxe.Log.trace.__hx_invoke2_o(0.0, haxe.root.Ext.test(), 0.0, new haxe.lang.DynamicObject(new java.lang.String[]{"className", "fileName", "methodName"}, new java.lang.Object[]{"Sample", "Sample.hx", "main"}, new java.lang.String[]{"lineNumber"}, new double[]{((double) (((double) (10) )) )}));
^
symbol: class Ext
location: package haxe.root
1 error
Compilation error
Native compilation failed
Error: Build failed
I would like to understand why the build failed. Any ideas?
I am not sure you might need to reference your Java code with -lib or something else?
But generally with Java target it's much simpler to just use a jar file. By typing haxe --help you will see the relevant command listed, I have never had a need to hand write externs for the Java target.
-java-lib <file> : add an external JAR or class directory library
The reason it fails is explained here
https://groups.google.com/forum/#!topic/haxelang/EHeoGN_Ppvg
I tried setting up with class paths and various options but did not get a solution, I think it's just a bit fiddly to do externs on the java target by hand. Really it's better to use Java compiler to create jars and let haxe auto generate the externs unless you get an issue then report it to hxJava repository.
Use -java-lib.
# build.sh
haxe Main.hx -main Main -java-lib javalib/ -java out
,
// ./Main.hx
import external.*;
class Main {
public static function main() {
trace(external.ExternalClass.myFunction());
}
}
,
// ./javalib/external/ExternalClass.java
package external;
public class ExternalClass {
public static String myFunction() {
return "External Java function";
}
}
,
./javalib/external/ExternalClass.class is the output of javac ExternalClass.java

Eclipse, change compiler command (for JOMP)

I want to use JOMP API (equivalent to OpenMP in C) but I met some problems:
This is the code I want to run:
import jomp.runtime.*;
public class Hello
{
public static void main (String argv[])
{
int myid;
//omp parallel private(myid)
{
myid = OMP.getThreadNum();
System.out.println("Hello from " + myid);
}
}
}
It is just an hello worl but I have a problem with the compiler. Please have a quick look at this page to understand:
http://www2.epcc.ed.ac.uk/computing/research_activities/jomp/download.html
But I can't, I do not understand how it works... I can only compile it with eclipse default compiler (I guess) and then I have only one thread!
I understand I have to compile this code (in a .jomp file) with
java jomp.compiler.Jomp MyFile
and then compile normally but I can't do this in ecplise neither in the terminal (I do not know how to install this compiler!)
ps: I use Ubuntu 12.04 on a Intel® Core™ i7-3610QM CPU # 2.30GHz × 8.
You just need to add the JOMP parameters to your launch configuration, this example can help you:
JOMP eclipse workaround

Error: Could not find or load main class hello.world.HelloWorld

I am trying to run this project called "hello user". I am new to Java, so wrote a simple program that takes your name, and displays "Hello ". while Running it, I get the following error:
run:
Error: Could not find or load main class hello.world.HelloWorld
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
But when I run file HelloWorld.java, it does it fine
I am doing this on Netbeans IDE 7.2
Rather than the coding error, it could be related to IDE. Since the "Run File" runs okay, but 'Run Project" does not, I believe you have something to set up in IDE itself. Right click the project, and select "Set is as Main", now run the project. I am just giving it a guess, may not help you. But it worth a shot.If it does not help, please paste your code too.
Your class needs a public static void main(String[] args) function. And moreover I suspect that the error could be in the package.
If you want your class in <main_package>.<sub_package>, The directory structure is
- main_package
- sub_package
-HelloWorld.java
And be sure to write your class like this.
package main_package.sub_package;
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello " + args[o]);
}
}
This is all due to the naming convention in Java
You need to run the .class file containing the public static void main(String[] args) method..
Here, your HelloWorld.java file might contain a class with main() method.. So, you can run it..
This is because, execution of any Java program starts with the invocation of main().. JVM needs an entry point to your code.. Which is main().. If it doesn't find one.. It will not run..
So, make sure, whatever class file you are running, it should have main() method..
UPDATE :- And for the starting point, may be you can skip using packages.. Just go with plain Java class without packages..
This message can also appear in Eclipse (Juno 4.2.2 in my case) and I have found two potential causes for it.
In my cases:
1. a DTD was in error. I deleted the file and that solved the issue*.
2. having cleaned the project, an external Jar that I had built externally had been deleted as could be seen from Properties -> Java Build Path -> Libraries.*
*Having solved either of the above issues, it was necessary to restart Eclipse
if you are using intellij idea then just rebuilding (clean and build) project might solve your problem . because intellij might be still trying to load the old classes which are not there or changed
Make sure you call looks like below:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello user");
}
}
To run a Java class in stand alone mode, public static void main(String[] args) is the entry method, which is must.

Categories

Resources