How to add Java file to Build Path Android Studio - java

I have some .java files that I'd like to include in my build path to utilize some of the classes and such inside them. I've added them to the /lib folder but it doesn't allow me to call data types and methods and such from the .java files in my app project (in the .MainActivity). How can I accomplish this?

The simplest thing is just to add the Java source code to your existing project, alongside your existing Java code.
In your case, the Java files do not have a package declaration. That's... not good. But, you can add one, then put the files in the appropriate directory for that package in your project. For example, you could use the package used by some of your existing classes, then put the files in the same directory as those classes.
If the Java files already had a package, you could create a directory tree off of your java/ directory based on that package, then put the Java files in that directory.

Related

Package vs Folder in Java. src\main\java doesn't need specifying package

I was watching a lecture on Java when I caught myself not specifying package in Java file.
They lecturer was using Java 11 on Netbeans, and I Java 1.8 on IDEA. By default Netbeans created him a package, and the main java file has package specified.
But on IDEA, I noticed that I don't need to specify package name, when creating .java file inside src\main\java folder. And I have a question why?
Why is src\main\java a folder, and not a package?
What is the difference between a folder and a package?
In Python, they create __init__.py file inside of a package, in order to make Python interpreter see the package. How is it working in Java, if there is no __init__ file? How does Java understand that this is a folder, and that is a package?
Why does Java need to introduce and separate folder and package terms?
Why is src\main\java a folder, and not a package?
Which folders become packages depends on where the "root" is defined. In IDEA, src/java/main is the root. Any folders inside of that are mapped to packages. If for example, you create a folder src/java/main/foo and create a Foo.java in that folder, you will have a class named Foo in a package named foo.
What is the difference between a folder and a package?
A package is a way to group related Java classes. Sometimes a package is implemented as a folder in your file system. Sometimes it is something else. (See below.)
Why does Java need to introduce and separate folder and package terms?
One reason to differentiate between folders and packages is that some packages aren't folders. For example, you can download a .jar file from the Internet which contains a Java library. The library inside that .jar file defines packages and classes that you can use in your own code, but there are no folders for the packages.
The reason for a new term "package" is to create an abstraction that isn't tied to the folders in your local file system.
The difference between a package and a folder is not directly evident unless you define your classpath yourself.
All directories and all zip files you add to the classpath create a baseline. Such as the .../src/main/java folder. Underneath that baseline you can still create folders with classes. But for Java these will be treated as packages.
So in the filesystem packages are resembled by folders. In zip files (and jar files are nothing but zip files with some metadata) there are no filesystem folders but still something folder-like that can be extracted into filesystem folders.
And it is thinkable that people would write even other Classloaders that do not just do simple filesystem or zipfile access. They can do virtually anything which could even range to loading classes from different websites - just based on their package names.
As soon as a class is not in the default package (in a folder on the classpath directly) it needs the package name declared in it's source code. And that is what you spotted. Well done!
Summary: .../src/main/javais the default package (that does not need to be specified) because that folder is added to the classpath when the JVM executes. Any classes in subfolders need their package defined.
A small detail: The classpath is set for the compiled classes, not the java source files. You could keep all java sources in the same directory and just declare by package into which 'folder' the compiled output should go. But it is good practice to organize the source files in the same structure that will be emitted by the compiler. Any IDE will help you maintain that structure.

How does java find .class files of java standard library or libraries I buy

I came across this question:
What is a classpath and how do I set it?
and the first answer explainns what classpath is and how to set it:
.....First, let's suppose that MyClass is something you built as part of
your project, and it is in a directory in your project called output.
The .class file would be at
output/org/javaguy/coolframework/MyClass.class (along with every other
file in that package). In order to get to that file, your path would
simply need to contain the folder 'output', not the whole package
structure, since your import statement provides all that information
to the VM.
Now let's suppose that you bundle CoolFramework up into a .jar file,
and put that CoolFramework.jar into a lib directory in your project.
You would now need to put lib/CoolFramework.jar into your classpath.
The VM will look inside the jar file for the org/javaguy/coolframework
part, and find your class.
So, classpaths contain:
JAR files, and Paths to the top of package hierarchies....
but if java only looks for classes in directories specified by CLASSPATH variable how does java find classes from libraries that are part of JRE?

Android: How can you "unpack" or "dump" files from a library project into the main project?

I would like to "dump" or "unpack" a library project's files into the main project, which would let me customize the library's contents for that project. Is that possible, and what is a way to do so? Can I just copy the entire library project's src/, res/, and manifest file into the libs/ folder of the main project?
(Please note that I do not want to touch the files from the original library project because I have other projects that are using it.)
If your library is a .jar containing .java source files, then you can unpack it using, eg. unzip, and move the contents of its src and res directories into your project's src and res directories.
If, however, this library contains only compiled Java .class files, there's not much you will be able to do with them. Unless you want to try to decompile them, the library must be used as-is.
I think that is better change the project name of the library and make a copy of this. This allow you to modify the files and dont mix it with your actual project.

can files in different folders be in the same package? <<java beginner confusion>>

I was looking through a sample project project provided by the android developer website and I see that a file Beam.java has been declared in a package as package com.example.android.beam; with the folder structure "..\src\com\example\android\beam\Beam.java". There are two other files, BuildConfig.java and R.java in a similar directory structure, but in a completely different directory "..\gen\com\example\android\beam\Beam.java" but with the same package declaration of package com.example.android.beam;. Are all these files in the same package even though they are in different folders? Did the IDE put the latter two files in a different folder only to make it clear that they are autogenerated, or is there a another reason as well?
The folder called "gen" is an automatically generated folder which contains files that are also automatically generated. In other words, don't worry about the "gen" folder, your packages and files and whatever you make will be in the "src" folder.
For example, the Android IDE generates the file "R.java" which contains the element id's used in your code, ie. "R.id.some_element", "R.string.some_string".
can files in different folders be in the same package?
Yes, as long as they all are compiled and packaged to be in the same directory in the .dex file.
They are autogenerated so as you said, they are put in /gen/... . What YOU write must be put in /src/...

custom file types in jar

I am developing a Java project in Netbeans which contains multiple files with custom types (like .rml .mod and ...)
The netbeans does not show these files in project, and when I build the project I need to copy them manually so the project can load them.
Is it possible to automatically include these files into output Jar file in build time?
If yes, then how and how can I access them in code?
If no, then how can I manage these files (automatically copy them to desired place and etc.)?
create a package inside java application and add files to this package.
for access to this files use of following code:
getClass().getResource("/Images/imgedit.png")
Package name instead of "Images" and file name instead of "imgeit.png".
You can use Ant for building the project
You can take basic reference from here and here.
If your data files are in the source folder they should be copied automatically to the bin folder along with the generated class files.
To read a data file from within a jar you could use getResourceAsStream(String) available in Class class.

Categories