How can I call methods from a different java project? - java

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.

Related

How to define a class or import one in a Scrapbook?

I need to test some code on the fly using the Eclipse Scrapbook. Is this even the best way to do it? I don't come from a Java background. Anyways, its frustrating me because I'm trying to define a simple class in Java:
public class RegistrationResponse {
public String message;
public Boolean success;
}
I want to use this class in my Scrapbook. So here's my project structure:
src
|-(default package)
| |- RegistrationResponse.java
|- Scrapbook.jpage
I went into my Scrapbook, and added the RegistrationResponse type from the Java Snippet Imports window. Then, when I run my code, it tells me this:
The import RegistrationResponse cannot be resolved
You gotta be kidding me. How do I do this? Anyways, I noticed I wasn't able to add the default package in my snippet imports. Is that why its failing? Is it supposed to be added by default or no?
Do not put classes into Default package. put it into a subpackage.
If you are using eclipse, you con use shift+ctrl+o to Import all referenced classes

How to write an import statement to refer to a java class in a jar?

How can I refer to a Java class in stdlib1.jar when the directory structure is like this? How to write the import statement?
I want to call a method under stdlib1.jar, I have configured it.
The classes are in the default package. According to this answer, it is not possible to import classes from the default package. So, they have to be moved to another package or you have to use reflection.
You call a method from a class and not from a package.
You don't need to specify the jar when you call a method from a class belonging to it. Which matters is your jar is in the classpath
In your screenshot if the lib makes part of the classpath folders, you can import and use classes from it in your code.
Here the classes of your jar use the default package (no package name) which seems weird for a third-party library. Default package is not recommended since it doesn't allow to naturally reference and use the classes of the archive from the client code.
I am not sure you are using the correct version of the jar. Look at that :
http://grepcode.com/snapshot/repo1.maven.org/maven2/com.googlecode.princeton-java-introduction/stdlib/1.0.1
This contains classes in the edu.princeton.cs package :
With package, you could declare this :
For example :
You could create a class like that and use BinaryIn like that:
package main;
import edu.princeton.cs.BinaryIn;
public class MyClass(){
public static void main(String args[]){
BinaryIn in = new BinaryIn();
}
}

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();
}
}

Need to import custom package to my class

Hi I have a custom class which consists of a custom exception folder and 3 .java files.
My structure is com/raeside/family/
So I want to do the following
import com.raeside.family.*;
But it is just giving me a red underline and when I try to let netbeans repair it, it doesn't work. Where should I store my com/ folder so that I can import it into my java program? I tried storing it inside of projectname/src/ but it's to no avail.
package com.raeside.family;
public class W4Q3 {
public static void main(String[] args) {
System.out.println("hello");
}
}
Is my code, my W4Q3 file is in the family folder. Is there something I am missing?
If all of your classes are in the same package (e.g. com.raeside.family) then you don't need the import statement at all. Make sure that all of your java files are in the src/com/raeside/family directory and have
package com.raeside.family;
at the top.

Java type cannot be resolved MIME type class

I know this is a common question asked but I've been searching and I've included the class into eclipse through the buildpath. I start to write the import statement and it autocompletes options for me so I know it's finding the class.
My problem is how come it's giving this error when I'm reading the docs and it says the constructor method is MimeUtil2() ?
http://www.jarvana.com/jarvana/view/eu/medsea/mimeutil/mime-util/2.1/mime-util-2.1-javadoc.jar!/eu/medsea/mimeutil/MimeUtil2.html#MimeUtil2()
package com.jab.app;
import java.io.File;
import eu.medsea.mimeutil.*;
public class CheckFileType {
private void GetMimeType(File filename){
MimeUtil2 test = new MimeUtil2(); //Produces the error saying java type cannot be resolved
}
I think you need to import
import eu.medsea.mimeutil.*;
According to the documentation, the type is eu.medsea.mimeutil.MimeUtil2
I ended up finding out that I was using the test-source.jar not the main jar file itself. The sourceforge page made the default as the source file instead of the main jar file.
It was buried inside of the files page.

Categories