Regarding how to use external libraries in eclipse (java) - java

I'm a complete beginner in Java using eclipse and even after installing those correctly external libraries,(I installed them in to my build path and they come in my referenced library section) which would make my job easy I can't use them for some reason.
import acm.*;
I used this to import all the classes of this library and when I tried to use those classes in my program, It didn't work for some reason.It gives me the following error if I try to use the method print() which is a method of the class IOconsole of this library.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method print(String) is undefined for the type ShortPrint
at ShortPrint.main(ShortPrint.java:5)
I don't know if I missed any steps but I'm pretty sure I have installed the libraries correctly,Just can't get them to use.
EDIT 1: Heres my program.
import acm.*;
public class ShortPrint {
public static void main(String []args) {
print ("hello");
}
}

You need to have an object of ShortPrint, like so
ShortPrnt sp = new ShortPrint();
sp.print("Hello");
I am guessing you are trying to call print like this:
ShortPrint.print("Hello");
which would only work is print was a static function of ShortPrint
Another possibility is that you do not inherit ShortPrint from IOConsole, this the IOConsole.print is not accessible from ShortPrint
UPDATE: after OP added code on usage, the suggestion is to add the import
import acm.io.*;
as the IOConsole class resides in the acm.io package. Then change the call to
IOConsole cons = new IOConsole();
cons.print("hello");
as print() is not a static member of IOConsole

I believe you should change your import to:
import static acm.IOConsole.*
Since it appears that the print() method is static in IOConsole.

Related

How can I call methods from a different java project?

It's as the title says. I'm trying to call methods defined in another Java project. Is there a way I can do that? I tried import statements but that didn't work.
EDIT:
So here is what is sitting in the code now in terms of imports:
enter image description here
and here are some of the functions I want to call that are in the other project:
enter image description here
What I've tried is:
import com.example.cs320EthicsPlayer.api.*
but that doesn't work it just says import can't be resolved.
Where the 2 projects are located:
enter image description here
I'm not too familiar with mvn directories but we are using maven. The methods I want to call are from the cs320EthicsPlayer folder (project) and the file I'm calling it from is from partyinthebackend (another project). I haven't called on the other project at all, and that's what I'm trying to figure out.
Class Path for the file I'm trying to call the functions from:
enter image description here
Let's say we have a class Test inside a package com.example :
package com.example;
public class Test {
public static String getHalloWorld (){
return "Hello world";
}
}
All we need if we want to use our class Test in another package is to use import like that
import com.example.Test;
class OtherPackage {
public static void main(String[] args) {
String geeting = Test.getHalloWorld();
System.out.println(geeting);
}
}
You should remember anything you want to use in another package it should be public.
So just check where is the method, which package and class include the method you are trying to import.
Let's fix your problem now:
try
import com.example*
Now you import the whole package, but you should remember you can import and use just the public method from the package example.
Update:
I see you have updated your question again, and you want to use maven, I think that will answer your question :
Java project dependency on another project
I hope that answers your question.
If it is in the same project but different package you can just doing
import package name...
If it is a complete different project you can't import them. You need to re-insert the methods.

Need help setting up intellij java project with multiple .java files from scratch

Edited to restart question from scratch due to complaints. I am a newbie to this format and to intellij so please excuse...
I am building a project in intellij for class. This project imports jnetcap and uses it to process a captured pcap file. My issue is I have two class files I am trying to integrate. NetTraffic which is the user interface class, and ProcessPacket that actually reads in the packet and does the work.
I have tried to make a project and import ProcessPacket into NetPacket but have been unsuccessful so far. I am sure I am missing something simple in this process but I just can not find anything showing the proper way to do this.
I have gotten it working by making a package under the src directory and adding both files to that package. This doesn't require an import from the NetPacket class and seems to work but my worry is that I need to be able to run this from a linux command line. I have been working all semester so far with everything in one source file so it hasn't been an issue until now. I don't remember using packages in the past under eclipse to do this.
Can someone offer a step by step process on how to properly add these source files to my project so that I am able to import ProcessPacket into NetTraffic or will leaving like this in a package work fine?
The files in question reside in package named nettraffic in src directory.
NetTraffic.java
package nettraffic;
public class NetTraffic {
public static ProcessPacket pp;
public static void main (String args[]) {
pp = new ProcessPacket();
pp.PrintOut();
}
}
ProcessPacket.java
package nettraffic;
import org.jnetpcap.*;
public class ProcessPacket {
public ProcessPacket() {
}
public void PrintOut() {
System.out.println("Test");
}
}
Note there is no real functionality in these at this time. Just trying to get the class import syntax correct before continuing. Again while this seems to work as a package I want to have it done without using a package and importing ProcessPacket.java into NetTraffic.java.
public class NetTraffic {
ProcessPacket pp = new ProcessPacket();
pp.PrintOut();
}
You're calling the PrintOut() method outside of any constructor or method or similar block (static or non-static initializer blocks...), and this isn't legal. Put it in a constructor or method.
public class NetTraffic {
public NetTraffic() {
ProcessPacket pp = new ProcessPacket();
pp.PrintOut();
}
}

How to use java methods defined in another class/file

I've been working on some problems from Project Euler, and, in the process, have written a lot of useful methods (in Java) that I might like to use in other Java projects. I want to be able to call them in the way that you call a function from java.lang.math, so if I had a method primeFactor() I could call it using MyMathMethods.primeFactor(number). How would I go about this? Would I make some kind of package that I could import? Would I make a superclass that includes all my useful math-y functions and have whatever class I'm working with in a new project extend that? There are probably multiple ways to do this, but I don't know what is best. Thanks in advance.
Mark your utility methods as public static. Package your classes containing those utility methods in a jar. Add/Refer that jar in your project, where you want to use the. Then in your code you can call them in a static way lke : MyUtilityClass.myUtilityMethod();
The best thing for this situation is to work in meaningful packages and make their jar
You can create a package like
/* File name : Animal.java */
package animals;
interface Animal {
public void eat();
public void travel();
}
Also on classes
package animals;
/* File name : MammalInt.java */
public class MammalInt implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
public int noOfLegs(){
return 0;
}
public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
You can import them like
import animals.*; OR be more specific import animals.MammalInt;Now you can make the jar file , import it in your project and use its methodYou can eaisly do it by this commandjar cmf MyJar.jar Manifest.txt MyPackage/*.class
For more details about jar creation please see thisAs a side note: Be carefull about visibility of members and functions while packaging itBecause there usage and accessibility matters a lot while we are using them
You could create separate java project with your util classes only and then create jar file and import into any another project.
Simply instantiate the class. Like your example, if you had a class MyMathMethods with the function primeFactor(number) then at other classes, simply instantiate it with something like private MyMathMethods myMathMethods;. Now, to call the function simply do myMathMethods.primeFactor(number); You may need to import its package as well.
False understanding of packages is any class defined within a package is visible to all other classes. Not true from my experience. If you have classes containing utility style methods you want to make available in another class? Simply declare a new instance of the class in the class you need the method in. Like... private MathUtilsClass mathUtilsClass = new MathUtilsClass(): Then any method you want to call from this class uses the new identifier, e.g. mathUtilsClass.greatFunction(); This is stupidly easy and should solve your problem.

Using BTrace to find when a class is created for the first time

I'm trying to use BTrace to find when a certain type is first instantiated in my program (Eclipse debugger isn't able to find it) as I'm seeing some strange behaviour (the Javolution XMLStreamWriterImpl is somehow adding elements to my XML before it should even have been created).
Anyway, I have the following method which I am using through JVisualVM, but nothing is showing up when running.
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
import java.lang.String;
#BTrace
public class ClassLoad {
#OnMethod(clazz = "javolution.xml.stream.XMLStreamWriterImpl", method = "<init>", location = #Location(value=Kind.NEW))
public static void site(#ProbeMethodName(fqn=true) String caller) {
println(strcat("Called from #", caller));
}
}
You need a different #OnMethod definition.
#OnMethod(clazz="/.*/", method="/.*/", location=#Location(value=Kind.NEW, clazz="javolution.xml.stream.XMLStreamWriterImpl"))
Basically you specify that you want to inspect all the methods of all the classes for occurrences of new javolution.xml.stream.XMLStreamWriterImpl instructions.
The rest of the code can stay the same.

Java (JNA) - can't find function in DLL (C++) library

I am new in Java, searched for this question in google and stackoverflow, found some posts, but still I can't understand.
I want to use DLL libary (C++) methods from Java. I use JNA for this purpose. JNA found my library but it can't find my method:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'LoadCurrentData': The specified procedure could not be found.
My code:
package javaapplication1;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
public class JavaApplication1 {
public interface LibPro extends Library {
LibPro INSTANCE = (LibPro) Native.loadLibrary(
(Platform.isWindows() ? "LibPro" : "LibProLinuxPort"), LibPro.class);
public short LoadCurrentData();
}
public static void main(String[] args) {
LibPro sdll = LibPro.INSTANCE;
sdll.LoadCurrentData(); // call of void function
}
}
I looked in my DLL with Depency Walker Tool and saw that my function name has prefix and suffix - it looks like _LoadCurrentData#0
Thanks for response!
P.S. I found good example which works http://tutortutor.ca/cgi-bin/makepage.cgi?/articles/rjna (Listing 6).
I'd say that you need to apply correct name mapper, as you noticed function name got mangled, you need to register CallMapper that will implement the same mangling as your compiler.
Here is a revelant entry from JNA homepage:
Use a dump utility to examine the names of your exported functions to make sure they match (nm on linux, depends on Windows). On Windows, if the functions have a suffix of the form "#NN", you need to pass a StdCallFunctionMapper as an option when initializing your library interface. In general, you can use a function mapper (FunctionMapper) to change the name of the looked-up method, or an invocation mapper (InvocationMapper) for more extensive control over the method invocation.
Here is a possibly revelant question: renaming DLL functions in JNA using StdCallFunctionMapper

Categories