custom file types in jar - java

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.

Related

How to add Java file to Build Path Android Studio

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.

How to make Java package with classes

I would like to make a Java package in a JAR file with precompiled classes such that other Java projects can consume these. How do I do that? It seems to me that most guides I have found expects a Main class/method to be available, but I do not want this to be an application that runs by itself. Furthermore, the resources (various files) inside of my project should be put into the JAR, since my app depends on these. Is this possible? I am (by the way) using Gradle.
A claim has been made that this question is a duplicate of this: Java creating .jar file. However, this question assumes the existence of main methods, and it does not concern how to include resources.
You can create the jar from the command prompt.
Copy all the classes that you want to include into a folder.
Then open that folder in command prompt and issue this command.
jar cfv YourProjectName.jar *
And a JAR will be created in the same folder containing all the classes.
Another solution:
If you are using eclipse try:
Right Click on the Package -> Export -> java -> jar file
You could also select the Classes and right click on them instead of the Package.
Edit:
Refer to https://docs.oracle.com/javase/tutorial/deployment/jar/build.html for more details on this command.
you can put all your methods/functions in class file then export it to .jar
then add the jar to your project's build path. Now you should be able to call those functions from your current main java class.

Create project from existing project Netbeans

I have downloaded some source code that contains src folders with some .java files. I want to try to run the code in netbeans.
This is the structure of the folder :
C4.5/src/main/java/myc45/
and in these folder include some .java files.
What should I do first?
When you create a project in Netbeans, one of the options in the project creation window is create project from existing source. If you have an existing project, you can also edit the project properties and tell it what the source folders are.
As an alternative to #PaulJAbernathy 's solution:
create a new project in Netbeans
via the projects windows, create a package myc45 (the package name used in the code you want to import) - you can do so by rightclicking, new Package
now, inside the src directory of your project directory you'll find a directory called myc45. Drop the source files into that directory using whatever file browser you commonly use. You'll see that Netbeans picks up the files almost immediately in the project explorer.
you can now use the code.
A bit messy, but there are advantages: if eg you want to transform a bunch of existing code files into a Maven type project, this is probably the easiest way.

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/...

Netbeans Clean & build classes

When I clean and build a project in NetBeans, the .jar file appears in the dist folder, like it's supposed to. But what if I have multiple files under the project? What happens to those files? E.g. I have a Game project, and under it are the different characters(knight, rogue, etc.) but I only see a game.jar file when I clean and build, I want to know what happens to the individual files. Thanks
Those files should be in the jar file as compiled .class files. It's easy to double check what's in the jar file since it's in zip format. You can use a program like 7-Zip to open it, or rename it to the zip extension (e.g. from mygame.jar to mygame.zip) and whatever OS you're using probably has some way to open it.
When you open or extract the jar file you'll find the compiled class files in a directory structure that reflects your package structure. For example, if you have Knight.java in the directory src/game/characters/Knight.java in the jar file you'll find something like classes/game/characters/Knight.class.
The name "jar" is an abbreviation of "Java archive". It stores all the classes and other resources (for example, images) in a project.
The classes you have defined in .java files will be compiled into .class files - these are contained in the .jar file.
All resources get compiled into the JAR file. If you want a separate JAR for the resources, you'll need to split the project into two maven projects: one jar for the code, one for the resources. You can then create a third project that would generate a distribution.
That's a lot of work, though. It's.a lot easier tO keep everything in one JAR unless you have explicit dynamic loading requirements.

Categories