How do you create java packages using notepad++? [duplicate] - java

This question already has answers here:
How to use Java packages? [duplicate]
(3 answers)
Closed 7 years ago.
I am trying to create a java file that will read in xml files but for me to do this i need to use a DOM Parser which involves creating a package and declaring it and i was just wondering how do you do that

Packages in Java classes reflect the folder structure of your project. For example, if your project is located in the project folder, and the structure of your project is:
project\
project\mypackage\
project\mypackage\MyClass.java
then the MyClass.java file should contain the corresponding package declaration:
package mypackage;
public class MyClass {
You should also read a tutorial on packages to better understand how they work: https://docs.oracle.com/javase/tutorial/java/package/index.html

A package is declared as the first non-comment line in your Java source file:
package an.example.pkg;
public class Foo {
// code here
}
Will declare a class named Foo in a package called an.example.pkg.
Of course, the package is associated with a folder on your file system, so your .java file would actually need to be located in a directory called <some directory>/an/example/pkg in your file system (where <some directory> is a directory that is in your Java classpath).
The package tells Java where to search to find your classes, i.e. in which folders relative to your classpath to look.

Related

Compiling java class from command line defined in a package [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 1 year ago.
I'm able to compile and run a simple hello world program from command line with
javac hello.java
java hello
however, if I had a package statement at the top package com.mypackage.myclass and try the same I get
Error: Could not find or load main class Reflection
What is exactly happening? And how do I fix it? Thanks.
UPDATE: Thanks everyone. I already had created the directory structure manually. In order for this to work I have to run java com.mypackage.myclass from the root directory, otherwise it will not work. Still don't understand the underlying mechanism. What is exactly happening?
If you have a java class like com.example.test.Main then the compiled class should be in a folder com\example\test and the class file name Main.class
You could manually do it, but compiler has a switch -d <target folder> which will create the folder structure for you.
javac -d . Main.java
In the above command the target folder is the current folder where you have the Java source file.
Once compiled you must use fully qualified name of the class to execute from the same folder you compiled the java file.
java com.example.test.Main
Screen Cap

Issue with conflicting Java packages [duplicate]

This question already has answers here:
Java: Which of multiple resources on classpath JVM takes?
(3 answers)
Closed 1 year ago.
I am facing some difficulties with this package concept of Java. Let's assume I have the following two directory structures.
dir1
|___com
|___example
|___mypackage
|___myclass.class
dir2
|___com
|___example
|___mypackage
|___myclass.class
So both the myclass.class files are part of com.example.mypackage. Now if both dir1 and dir2 are on CLASSPATH and I write import com.exaple.mypackage.myclass, which path will be considered?
So, It is the responsibility of class loader to load classes. So first of all custom classes have been looked up by the JVM then it goes jar files to check the classes.
In your case it looks like both are custom classes hence to resolve this issue you have specify which file you want to load then you can add dir1 / dir2 to specify the exact class.
JVM picks the latest classes you entered in hierarchy.

How to call java classes of a compiled jar file within android studio?

Hello StackOverflow folks,
I have a jar file of java classes. I added this jar file to my android studio project under folder /libs. Now, what I want to do is use those classes within the jar file in MainActiviy.java. I just do not know how.
Some details:
My jar file is named: zombi.jar.
The class within the jar file to call is named: COMBI.class
I tried the following:
In MainActivity.java, I wrote:
// declared class variable
private COMBI mCOMBI;
Then, in OnCreate method, I wrote:
mCOMBI = new COMBI();
//to start calling method COMBIStart to launch the command-line system
mCOMBI.COMBIStart();
I actually called the classes as I would in normal Java. I think Android uses special java code that looks like java, but I don't know how to use them.
I could not get the code to work.
Can you help me?
I'm going to assume you've already written your import statement for the jar class. If you already put the jar file in the /lib folder, Android Studio should update your build.gradle file. Check to see if you have a link to the jar file in your dependencies{...}
If not, you can add it manually.
Just like in normal java you have to add an import to the correct package at the top of your file for everything that's not in the same package the file is in.
It should look something like this:
import android.view.View;
but instead of android.view.view it would be a reference to the class in the jar you're trying to add

Running a java program with multiple jar files and classes [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 4 years ago.
I am compiling a program with multiple jar files (inside the lib folder) and classes (inside the src/com folder) with:
javac -classpath lib/\* src/com/*.java
I typed this to run the program:
java -cp lib/\* src/com/okc
But it doesn't work. Instead, I get this:
Error: Could not find or load main class src.com.okc
okc.java is the class containing the main method. How can I run a java program with multiple jar files and classes?
A Java class file is not just the file itself. The directory structure which represents a class's package is part of the class file. Your classpath needs to point to the directory which is the parent of the topmost package directory.
Assuming your class is declared with package com;, the topmost package directory is com. So you need the parent of com in your classpath:
java -classpath src:lib/\* com.okc
If your class does not contain any package statement, and you just happened to put it in a com directory, then it belongs to the null package, whose parent directory is com itself:
java -classpath src/com:lib/\* okc
An additional note: It is Java convention to have class names, and their respective file names, start with an uppercase letter. One reason is that it makes class names easy to distinguish from package components.
Try:
java -cp ../lib/\* com.okc
from the src directory (not sure...)
Assuming that your current directory has your lib/ :
java -cp lib src.com.okc

How to use jar files without package information?

I have a jar called "MyTools". The jar is in c:\data folder. I created a new file in the same folder called "UseTools.java". Now I would like to use some of the classes from the MyTools.jar in my UseTools.java. I tried this but it doesnt seem to work:
import MyTools.*;
public class UseTools
{
public static void main(String[] args)
{
MyTools.SomeClass foo = new SomeClass();
SomeClass.doSomething();
}
}
I tried to compile this with:
javac -cp . UseTools.java
and got this error message:
UseTools.java:1: package MyTools does not exist
import MyTools.*;
^
UseTools.java:7: package MyTools does not exist
MyTools.SomeClass foo = new SomeClass()
^
2 errors
I did not set the package name in any class.
Do I have to set a package name in my jar classes?
To mention something that relates more to the title of the question:
In Java, you can't access classes in the default package from code within a named package.
This means, if the classes in your jar file do not belong explicitly to any package and inside the jar your files are directly in the root folder without subfolders, they are in the default package. This is not very elaborated and lacks modularity as well as extensibility, but is technically alright.
Then, you can only use these classes from code which also is in the default package. But this does not necessarily mean it has to be in the same jar. If you have multiple src or class folders they could all contain classes in the default package which can interact. The organization in JAR files and the package structure in your project are independent of each other.
However, I'd strictly encourage you to use explicit package information.
In your MyTools.jar there should be a package with the name MyTools. And before compiling you should add the jar to the classpath.
You need to add -cp file.jar instead of -cp .
The latter one will pick up .class files only. BTW: why not using an IDE like netbeans, eclipse or intelliJ?

Categories

Resources