Compile .java file with external dependecy into .class file - java

I have the following External.java file that has an external dependency on JAsioHost.jar file placed in folderWhereMyJarIs:
package external;
import com.synthbot.jasiohost.*;
public class External {
public External(){
System.out.println("Class CONSTRUCTOR");
}
public int operateAdd(int a, int b){
int res = a+b;
return res;
}
public static void main(String[] args) {
System.out.println("Hello world");
}
}
I am having trouble compiling the .java file into .class file from my Windows command line because when I type
javac -cp .;/folderWhereMyJarIs/JAsioHost.jar External.java
it gives me the following error:
package com.synthbot.jasiohost does not exist
What am I doing wrong?

As suggested by #shadowsheep in his helpful comment, I answer my own question posting the solution that works just fine:
javac -cp .;./folderWhereMyJarIs/JAsioHost.jar External.java
Hope this will help others

Related

Unsatisfied link error inside intellij

I am using JNI (Java-Native-Interface) to acess some code that was written in c++. Everything works fine when I run the code from the command line but when I add the code to my Intellij project (class, header-file and dll) it gives the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\Robin\Desktop\egdb_example\end.dll: Can't find dependent libraries
My Java Code looks like this :
public class EndGame {
public static native void openDataBase(int maxPieces, int buffer);
public static native void test();
public static native int lookUp(int condition, int BP, int WP, int K, int color);
public static native void close();
static {
System.load("C:\\Users\\Robin\\Desktop\\egdb_example\\end.dll");
}
public static void main(String[] args) {
System.out.println("Kleiner Test");
openDataBase(8, 2000);
int value = lookUp(0, 0, 0, 0, 0);
close();
}
}
I checked whether the path is actually correct running this piece of code
File test = new File("C:\\Users\\Robin\\Desktop\\egdb_example");
for (File file : test.listFiles()){
System.out.println(file.getName());
}
which produces the outcome:
.git
.gitattributes
.gitignore
egdb.h
egdb.lib
egdb64.dll
egdb64.lib
egdb_example.cpp
egdb_example.vcxproj
egdb_example.vcxproj.filters
end.dll
EndGame.class
EndGame.h
EndGame.java
jni.h
jni_md.h
main.cpp
Readme.pdf
robin.cpp
robin.exe
Would appreciate any help
Take a look here to see how to setup IntelliJ and CLion to debug code.
http://jnicookbook.owsiak.org/recipe-No-D002/
If you don't use CLion, simply skip that part and take a look solely at IntelliJ configuration.

java mysterious classpath behavior

I failed specifying the class path. Here's my setup:
File: "root/src/hello/German.java"
package hello;
public class German {
public void greet() { System.out.println("Hallo"); }
}
I compile this in "root":
> javac root/src/hello/German.java -d root/package
where "root/package/hello" exists as an empty directory. Fine. Now I want to test and write
File: "root/test/testHello.java"
import hello.German;
public class helloTest {
public static void main(String[] args) {
German guy = new German();
guy.greet();
}
}
I compile
> javac testHello.java -cp ../package
To summarize, I have:
root/package/hello/German.class
root/test/helloTest.class
I execute in "root/test/":
> java testHello => class not found except.
> java testHello -cp ../package => class not found except.
> java testHello -cp ../package/hello => class not found except.
However, copying the 'hello' directory into test such that there is
root/test/hello/German.class
root/test/helloTest.class
I can execute in "root/test/"
> java testHello
and it greets friendly in German. I want to specify the classpath, though. But, I have no idea why '-cp' and '-classpath' is not accepted.
Try this:
java -classpath .:../package testHello
.:../package to use the current directory and ../package as classpath.

How to get path to class file in eclipse and linux in java?

In eclipse for windows, when I run
public class HelloWorld {
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir"));
}
}
It gives me the path of the project root folder (which contains the bin folder which has the class file). For example
SampleProject
and the class file is actually located at
SampleProject\bin\myclass.class
But if I run the same program in linux with
javac myclass.java
java myclass
it gives me the directory that has the .class file, which is the same as pwd command. This is what I want in eclipse for windows. I want some code that will give me the path to the class file in both eclipse for windows and linux.
Does anyone know how do this?
Thanks
If I understand you correctly, you'd like a method that retrieves a class' path on disk. This is easily achievable, like so:
public String getClassPath(Class c) {
try {
return c.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
NOTE this will work even if the class is contained in a jar file. It will return the path to the jar in this case.
The easiest way is to do this:
public class HelloWorld {
public static void main(String[] args) {
System.out.println(HelloWorld.class.getResource("HelloWorld.class"));
}
}

How to call a class from another in th same package shows an error how to fix it in java?

How to call a class from another in th same package shows an error how to fix it?
i have three classes
FirstPack.java
SecondPack.java
Main.java
Here is my FirstPack.java
//FirstPack.java
package mypack.in;
public class FirstPack
{
public void fun()
{
try
{
System.out.println("First Package");
}
catch(Exception ae)
{
}
}
}
Here is my SecondPack.java
//SecondPack.java
package mypack.in;
public class SecondPack
{
public void fun()
{
FirstPack f=new FirstPack();
f.fun();
try
{
System.out.println("Second Package");
}
catch(Exception ae)
{
}
}
}
Here is my Main.java
//Main.java
import java.lang.*;
import mypack.in.*;
class Main
{
public static void main(String args[])
{
try
{
//FirstPack obj_fp=new FirstPack();
//obj_fp.fun();
SecondPack obj_sp=new SecondPack();
obj_sp.fun();
}
catch(Exception ae)
{
}
}
}
On compiling Firstpack.java ie javac FirstPack.java - no problem
On compiling SecondPack.java ie javac SecondPack.java- Error ....
C:\JAVASAMPLE\Package\Three\mypack\in>javac SecondPack.java
SecondPack.java:7: cannot find symbol
symbol : class FirstPack
location: class mypack.in.SecondPack
FirstPack f=new FirstPack();
^
SecondPack.java:7: cannot find symbol
symbol : class FirstPack
location: class mypack.in.SecondPack
FirstPack f=new FirstPack();
^
2 errors
And, on compiling Main.java ie javac Main.java - no problem
Package - mypack.in FirstPack.java and SecondPack.java Outside mypack.in Main.java Sir if without package yes it workinf but with pakage it didnt working I am not using any IDEs
[EDIT]
Note, you should not be compiling each class independently. The Java compiler is designed to compile each class as it encounters the first instance of it in your source file. Compile your Main.java from the root directory, and you won't have any errors.
[/EDIT]
[SECOND EDIT]
I believe this is all happening because the javac compiler begins looking for classes in the current directory and then searches through sub-directories. Since FirstPack is part of the mypack.in package, it needs to be in a folder ./mypack/in/ beginning from the current directory (the directory you execute the compiler from. Basically, your compiler knows that FirstPack is actually mypack.in.FirstPack and will begin looking for ./mypack/in/FirstPack from wherever the file you are trying to compile is residing.
http://kevinboone.net/classpath.html
[/SECOND EDIT]
Although it is throwing an error on compilation, this program is executing properly for me. Ensure that Main.class is in the root directory, and that FirstPack and SecondPack are in %root%\mypack\in
I can't tell you exactly why all of this is the way it is, but I know it's working this way.
You can do it like this:
///**Import the Package
import mypack.in.FirstPack;

exception in thread"main"java.lang.NoClassDefFoundError:

package p1;
public class test_package{
public void show(){
System.out.println("package1");
}
public static void main(String args[]){
test_package t=new test_package();
t.show();
}
}
Here is the first class made that has been compiled and package is saved in D: directory...it is running well...
now...
package p2;
import p1.test_package;
public class test_package2{
public void show(){
System.out.println("package2 in c:");
}
public static void main(String args[]){
test_package2 T=new test_package2();
test_package T1=new test_package();
T.show();
T1.show();
}
}
here is another class importing first class and this is saved in C: directory...
i have set temperory path using cmd command
set classpath=D:
and compiled it from C: using command
javac -d C: test_package2.java
when i am running it by command from C:
java p2.test_package2
it is throwing the error
exception in thread "main" java.lang.NoClassDefFoundError
but the .class file is in p1 package that is in D: drive.....
please help me if anyone has the solution.
It's because JVM is not able to find the test_package in the class_path of test_package2 which is D:. Add test_package to the build path of test_package2. In eclipse you can do this by Right-click on test_package2 > Build Path > Configure Build Path > Add External Class Folder (or you can zip test_package and use as an external library as well). Now import it in test_package2. It should run.

Categories

Resources