Need to import custom package to my class - java

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.

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.

How to create a single jar from multiple packages?

I want to create a single jar file from multiple packages. I have created the jar using below command but when I'm importing it to a project as a dependency it is not working.
jar cfe output/jar/my-java.jar Main src/pkg1/pkg0/*.class src/pkg1/*.class src/pkg2/*.class
My project structure is something like below structure
src
pkg1
A.java
B.JAVA
pkg0
E.java
pkg2
C.java
D.java
My Example code is something like
import pkg1.A;
public class Main {
public static void main(String[] args) {
A.printMe("Hello World");
}
}
error that I'm getting is:
java pkg1 not exist
But in the editor(IntelliJ), it is not showing errors and also i'm able to import class but not package.
import pkg1: showing red means error in the editor
import pkg1.A: not showing red means no error in the editor
Note: I don't want to use maven.
An unzip -t something.jar shows the actual file structure of the jar file (zip). It is the same as the class structure of it (except that instead "/", a "." is the separator).
In your case, the problem will be that src will be on the top level, and not pkg1. Either import src.pkg1 (very dirty), or play a little bit more with the directories / jar flags.
just call your method it automatic get path if you correctly put your jar in your current project.`project 1
pkg com.test.demo
class test{
public static void m1(){
System.out.println("project 1 in method 1 );
}
}
in project 2 put jar of project 1
pkg com.test.demo
class Test1{
public static void main(String...){
System.out.println(test.m1())
}
}

How to import a custom package into another? Getting error package "packagename" does not exist

So right now in my code I have this:
package pokemonbattle;
import pokemon.Pokemon.*;
public class PokemonBattle { //Do stuff }
and then I also have:
package pokemon;
public class Pokemon {
public static void main(String[] args) {}
public String getName(int pokemon) {
//stuff
}
Except that I'm getting the error "Package pokemon.Pokemon does not exist." When it does. The file name it's under is called Pokemon, but I've tried using different capitalization but nothing is working. Any suggestions? Thanks!
Packages are like directories and you should put your files into the place specified by their package. In each directory can either be files (classes, interfaces, ..) or other sub directories.
When you do
package pokemon;
public class Pokemon {
You express that this is the Pokemon.java file in the \pokemon directory. In other words \pokemon\Pokemon.java.
To use that class in another place you do either
import pokemon.Pokemon;
to import just that one class specifically or you do
import pokemon.*;
to import all the classes in that package (but not sub-packages thereof) When you say pokemon.Pokemon.* it's looking for a directory named \pokemon\Pokemon\ which doesn't exist.

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

import from external lib jython

I am trying to imported a java class from an external lib in jyhon and it does not work. An example
package run;
import import.Imported;
Class Run()
{
public static void main(String[] args){
pi = new PythonInterpreter(null);
pi.execfile('script.py');
}
}
//this is an external libary
package import;
Class Imported()
{
//some stuff;
}
//py script
from import import Imported //this line throws an error Module not found
#do some stuff
The strangest thing is that it runs when it is compiled in Eclipse, but does not from command line.
Any help?
Sounds like your classpath is probably set incorrectly at runtime. The easiest solution is typically just to add the directory or jar file containing 'import' to sys.path.
(Also, naming your packages 'import' is just asking for trouble.)

Categories