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())
}
}
Related
I have two folders pack1 and pack2 in my desktop.
pack1 contains file1.java file and the code is:
public class File1 {
public static void main(String[] args){
}
}
and pack2 contains file2.java and the code is:
import pack1.File1;
public class File2 {
public static void main(String[] args){
}
}
I successfully compiled file1.java and file1.class is created in pack1 but I got error while compiling file2.java:
error: package pack1 does not exist
import pack1.File1;
^
1 error
from the above error what I thought is I can only import class/package which are present in same or sub-directories because Pack1 is not in the same directory in which I am compiling(current working directory = $HOME/Desktop/pack2) please correct me if I am wrong.
If my understanding is correct how can I import file1 in file2.
You have to understand:
"the real" class name is always the.complete.package.WhateverClass
there is no concept of "sub packages".
Sure, from a directory perspective the classes for a.b and a.c will all sit in folders within the a directory, but for anything inside a.b, it still has to use the full name a.c.Whatever
Thus your import always look the same, like:
import com.pack1.subpack2.TheClass
In other words: when your code sits in a.b, there is no difference in importing from a.c or x.y.z, you simply always give the full class name, with all the packages that belong to it.
I am writing some Java code without IDE, I got a little problem when I try run the code after compiling it. (I am using Ubuntu 64)
$ javac ClassName.java
$ java ClassName
Could not find or load main class ClassName
My directory structure is the following:
Projectname
----- PackageName
---------- className.java
---------- className.class
My code start by writing down the packageNmae. When I remove the package statement, it works. While error occur when that statement is included.
package PackageName;
public class myClass {
// .... to be used in the main class
}
public class ClassName {
public static void main(String args[]) {
// ....
}
}
Could anyone tell me what is the problem.
The main issue is where are you trying to run the command from. You do not run it from inside the package directory, but from the root of your package tree (in your example, the Projectname directory).
From there, you should be doing:
javac PackageName.className
which tells it to compile "className"[sic] inside package "PackageName"[sic]. The way you are doing it, you are telling it to compile a class that is not part of a package (which is strongly discouraged).
Notes:
Each file can only have a "general" class, with the name of that file.
You may define inner classes inside a class, but that would be inside the code block of the class.
package packageName;
public class ClassName {
public class InnerClass {
...
}
public static void main(String args[]) {
...
}
}
File name and class name must be the same. That includes case (lowercase or uppercase) of the name.
Class names always begin in uppercase.
Package names should be in camelCase.
Usually you do not want to leave your compiled (.class) files with the source (.java) files. The usual structure is, at the minimum:
--> Project --> src --> myPackage --> MyClass.java
--> bin --> myPackage --> MyClass.class
so you only need to copy your .class files to distribute the executable.
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.
When preparing for the SCJP exam, we were going through the following code:
package certificaton;
public class OtherClass
{
public void testIt()
{
System.out.println("otherclass");
}
}
And this:
package somethingElse;
import certification.OtherClass;
public class AccessClass
{
public static void main( String args[])
{
OtherClass o= new OtherClass();
o.testIt();
}
}
I placed both the above files in the following directory: C:\scjp\temp8 ; and the strange thing is that, the .java files are compiling and results in two .class files being created in the same directory. The thing I want to ask, is that, the difference between packages and directory. Isn't it true that the class files could be created in a directory other than the one stated in the package declaration? And the package declaration is something 'virtual', and disregards the windows directory structure. In addition, isn't it also true that, by executing the following command:
javac -d . OtherClass.java
The directories are created conforming to the package declaration, which isn't always mandatory?
The directories are created conforming
to the package declaration, which
isn't always mandatory?
No, the package and directory structures must match. It's mandatory, not optional.
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.)