Custom library gives "no package found" - java

I decided to make a custom .jar library just for the hell of it.
It's insanely basic, it's package name is "add" and looks like this:
public class addtogether{
public void addtogether(int a, int b, int c){
c=a+b
}}
So I try importing it in a file, by doing this:
public class test{
import add.*;
public static void main (String[] args){
int x;
add(4,5,x);
System.out.println(x);
}
}
and surprise surprise! No package found.
I did the whole dance of adding it to the IDE in the preferences section, but it still doesn't work.
help please.

A very naive step by step implementation for using a custom jar:
Create a java project with a utility class containing your functionality. (You can use the class defined above.) Example: (Project Name: ArithmeticUtility, Jar Name : ArithmeticUtility.jar, Package name: com.arithmetic, ClassName: Calculator)
Export it as a jar.
Create another java project add the jar by right clicking on the project-> Build Path-> Configure Build Path->Add External jars-> Apply-> Ok.
Use the class in your present project by mentioning the required imports. (Example: import com.arithmetic.*)
Please edit your code as below:
import add.*; // import statements should be at top after package outside
public class test{
public static void main (String[] args){
int x;
addTogether testObj=new addTogether();
int result=testObj.add(4,5);
System.out.println("Result: "+result);
}}
public class AddTogether{
public void addtogether(int a, int b){
return a+b; // your method has no return statement
}}
Hope it helps.

Related

Custom Package Importing Issues

I am trying to create custom packages and i put in my other Program But when i import this Package that it will gives me error.
This is Custom Package File :
package Collage.Softwere;
public class IT{
public void meth1(){
System.out.println("This is method 1...");
}
public static void main(String[] args){
IT obj = new IT();
obj.meth1();
}
}
This is Test file where I Import This Package :
import Collage.Softwere.IT;
public class Test{
public static void main(String[] args){
IT obj = new IT();
obj.meth1();
}
}
I get This error :
error: meth1() is not public in IT; cannot be accessed from outside package
obj.meth1();
^1 error
MY directory set up is :
-->Package (Folder Name)
--> College (Root Directory)
-> Software (Sub Directory)
* IT.class
* CE.class
-> Hardware (Sub Directory)
* ME.class
* AME.class
You have a typo in the imports.
Collage.Softwere -> Collage.Software
Collage.Hardwere.ME -> Collage.Hardware.ME
if it isn't that typo, you need to make sure in your dependency management that the package is added as a dependency in the right pom.xml (if you're using maven).

How can I create .JAR to Do a specific task , then use this JAR in another project?

e.g : i have a simple code that take an array list of numbers and computes Sum. I am created a .JAR of this code. now my question is how can I import this JAR in another project and pass array list to it , and JAR give me the result to reuse it ??
This is an example
1- in a directory com\mycompany\myproject create Task.java
package com.mycompany.myproject;
import java.util.*;
public interface Task{
public int sum(List<Integer> list);
}
2- in a directory com\mycompany\myproject\support create MyTask.java
package com.mycompany.myproject.support;
import java.util.*;
import com.mycompany.myproject.Task;
public class MyTask implements Task{
public int sum(List<Integer> list){
int variable = 0;
for(int i: list){
variable += i;
}
return variable;
}
}
3- compile both .java with command $javac com/mycompany/myproject/Task.java and command $javac com/mycompany/myproject/support/MyTask.java
4- create .jar file with command $jar -cvf task.jar com/mycompany/myproject/Task.class com/mycompany/myproject/support/MyTask.class
(I decided to put "task" as name of my .jar file)
At this point you have created you .JAR and you can use it in another project. Let's see how to do it.
5- take your task.jar file and put it where you have defined your CLASSPATH System Variable
6- create Main.java in any directory.
import java.util.*;
import com.mycompany.myproject.*;
import com.mycompany.myproject.support.*;
public class Main{
public static void main(String arg[]){
//create the implementation you want
Task task = new MyTask();
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(8);
list.add(9);
list.add(10);
list.add(2);
int result = task.sum(list);
System.out.println(result);
}
}
7- comnpile Main.java with $javac Main.java
8- take Main.class(result of compilation of Main.java) and put it where you have defined your CLASSPATH System Variable.
9- go to your CLASSPATH directory and execute command $java Main
Assuming you have sum.jar Try running your application with -classpath sum.jar This will make possible importing classed/calling the code in the sum.jar from your application.

org.openide.util.Lookup Cannot Find Any Classes Implementing

SQLUtils.java:
import org.openide.util.Lookup;
import java.util.ServiceLoader; // This doesn't work either
public class SQLUtils {
public static DBDriver getDriver(String prefix) {
for(DBDriver e : Lookup.getDefault().lookupAll(DBDriver.class)) {
System.out.println(e.getPrefix());
if(e.getPrefix().equalsIgnoreCase(prefix)) {
return e;
}
}
return null;
}
}
MySQLDriver.java:
public class MySQLDriver implements DBDriver {
#Override
public String getPrefix() {
return "mysql";
}
}
DBDriver.java:
import java.io.Serializable;
public interface DBDriver extends Serializable {
public String getPrefix();
}
Main.java:
public class Main {
public static void main(String[] args) {
DBDriver d = SQLUtils.getDriver("mysql");
}
}
This does nothing when running it, it cannot find any classes implementing.
What the program is trying to do is get the driver that is entered as a parameter for SQLUtils.getDriver(String prefix) (in Main.java).
For some reason I cannot get this to work.
I'm not familiar with OpenIDE Lookup mechanism, but I am familiar with the Java ServiceLoader mechanism.
You need to provide a file in the META-INF/services/ folder describing what classes implement specific interfaces. From the Java Docs describing the ServiceLoader class is this example:
If com.example.impl.StandardCodecs is an implementation of the
com.example.CodecSet service then its jar file also contains a file
named
META-INF/services/com.example.CodecSet
This file contains the single line:
com.example.impl.StandardCodecs # Standard codecs implementing com.example.CodecSet
What you are missing is a similar file that needs to be included on your classpath or within your JAR file.
You don't include you package names so I cannot provide a more direct example to help solve your problem.
I dropped the NetBeans API and switched to Reflections. I implemented Maven and ran it with IntelliJ. Works well for me.

Intellij14.1 Error:No main class for module

I have just try HelloWorld
public class hw {
public static void main(String []args){
System.out.println("HelloWorld");
}
}
but, console said when I tried to compile:
Error:No main class for module: HelloWorldTest
Error:Compilation failed
I don't know I don't know what I have wrong, Why it give me this warn ?
Create a file HelloWorld.java at the root of your source package:
public class HelloWorld {
public static void main(String... args) {
System.out.println("HelloWorld");
}
}
Then from the project explorer (left pane), right click on your HelloWorld class, and select Run 'HelloWorld.main()'
Do you have the Haxe plugin installed?
If so it's likely the source of the problem.
https://devnet.jetbrains.com/thread/435604?tstart=0 and https://devnet.jetbrains.com/thread/435708
Try the fix in the first link or uninstall the plugin.

Using DLL in java through JNA

I am using dll in java using JNA, but i am getting below error
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'GetStatus': The specified procedure could not be found.
Not getting how to resolve this issue?
Please help.
Here is java code
import com.sun.jna.Library;
import com.sun.jna.Native;
/** Simple example of native library declaration and usage. */
public class First {
public interface TEST extends Library {
public String GetStatus();
}
public static void main(String[] args) {
TEST obj = (TEST ) Native.loadLibrary("TEST ", TEST .class);
System.out.println( obj.GetStatus());
}
}
This Nugget is super easy to use and works perfectly. https://www.nuget.org/packages/UnmanagedExports
You need Visual Studio 2012 (express).
Once installed, just add [RGiesecke.DllExport.DllExport] before any static function you want to export. That's it!
Example:
C#
[RGiesecke.DllExport.DllExport]
public static int YourFunction(string data)
{
/*Your code here*/
return 1;
}
Java
Add the import at the top:
import com.sun.jna.Native;
Add the interface in your class. Its your C# function name preceded by the letter "I":
public interface IYourFunction extends com.sun.jna.Library
{
public int YourFunction(String tStr);
};
Call your DLL where you need it in your class:
IYourFunction iYourFunction = (IYourFunction )Native.loadLibrary("full or relative path to DLL withouth the .dll extention", IYourFunction.class);//call JNA
System.out.println("Returned: " + IYourFunction.YourFunction("some parameter"));
EDIT:
If the DLL is 32bit, the JDK/JRE has to be 32bit as well. Add the following check to your code:
if(!System.getProperty("os.arch").equals("x86")) {
throw new Exception(".NET DLL " + "32bits JRE/JDK is required. Currently using " + System.getProperty("os.arch") + ".\r\nTry changing your PATH environement variable.");
}

Categories