Eclipse - Using a .class file in Java - java

As part of a project I was given three files, a partially written .java file where my work will go and two separate .class files. I created a new Java Project and added the .java file and started adding/editing the starter code.
How/where should I put the .class files?
I tried adding the class files to the bin folder and dragging/dropping them into the package in Eclipse.

You should go to the project's Java Build Path property page and add a Class Folder for those two files. The contents of bin will be erased and overwritten without notice.

Related

How to add your own .class into a compiled .jar file?

I have a .jar and I would like to add my own .class to it and recompile. Is there anywhere I can do it?
Yes, and it's simple (if you already have the .class file - then there is no recompilation).
(Assuming you have the .class file already and just want to add it to a .jar. If you don't have the .class file, you need to write a .java source file and compile it to .class first using javac)
Jar files are actually zip files - you can use zip/unzip to create and unpack them.
unzip the jar file using a unzip program
add your class to the unzipped directory (*)
zip again (possibly to another name.jar)
(*) In step 2, you must put your .class file in the correct directory - correct meaning that the package name of your class must match the directory path where you .class file resides, relative to the .jar archive root.
For example if your My.class defines that it is in package com.nicky, then it must be found at com/nicky/My.class (where com is a directory in the archive root directory). If My.class has no package, they it must be in archive root directory.
No recompilation is needed - Java does linking dynamically at runtime - if the rest of the program needs to use class com.nicky.My, it will do so successfully if the class file is in the correct place in the .jar file.
You use the classpath parameter of the virtual machine (java -cp a.jar:b.jar:... ) to tell the java process where to look for classes - in case it would make more sense to package your class(es) in a separate .jar file..
--
Edit 1 / responding to your comment:
In case you a writing a new .java file, you need to first compile it into a .class using the javac command line compiler that comes with the jdk.
Every Java class belongs to a package, which is usually declared in the first line of the .java source file. Package names must match directory path location of the class (like in the example above), for both .java source files (for compilation to succeed), and for .class files (for dynamic loading / running to succeed).
You could also download and use a Java IDE (eg. http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/mars2 )to create your .class file. Create a new Java Project in Eclipse, write your class source code, click build (if it already doesn't build automatically) and then use a file explorer to take your .class file from the 'target' directory of the project on the disk.
If your new .java class depends on classes from the .jar you are trying to add it to, use
javac -cp your.jar com/nicky/My.java
to tell the compiler where the additional compile-time dependencies are.
If you are compiling from Eclipse, you need to configure that your project depends on your.jar: Right Click on your Project -> Properties -> Java Build Path -> Libraries -> Add Jars..

How to compile jar file containing only .java source files into .jar files containing .class files with Eclipse

I have a .zip file that contains a package that holds the different source code files generated from http://www.jsonschema2pojo.org/.
I initially thought I'd be able to add the generated code (contained in .zip file) to my build path directly, however I discovered that I cannot work with .java files in my project, as importing the .zip file into my project shows the package path to be empty.
It turns out that I cannot use this .zip file because it is full of .java source files and it has no .class files.. which is why when I attempt to reference the file from my project, it says it can't resolve it to a type.
I know I can just go through the zip file, create the package and classes and copy/paste the source code, compile all of that, and then reference that in my program. However, there has to be an easier way than just doing all of this manually.
How do I convert a .zip file full of source into a .zip file full of class files directly referenceable from my Eclipse?
There is no easy way of achieving what you are looking for, I would recommend to write up a utility program which can take input as ZIP file, iterate the file list, read and create the package folder structure and copy the file to the location. Before executing this program, you have to create eclipse Java project so that the extracted files can be copied to the workspace. This way it functions properly when program generates the file and then you can extract this package in .jar file.

why xml files in eclipse project's source directory is copied to target/classes directory?

I have xml files in eclipse project's source directory, like:
src/java/main/com/xx/zz.xml
1.When using eclise to build automatically, xml files are copied to target/classes.
2.When using 'mvn complie', xml files are not copied to target/classes.
For the second case, I found this:why xml files in eclipse project's source directory is not copied to target/classes directory?.
but for the first case, I cannot find any document.
Can someone explain it for me ?
Thanks!
Eclipse works quite a bit differently than standalone Maven. Maven uses javac from JDK. By default javac only processes .java files and generates .class files in the same directory as .java sources. Maven asks it to generate classes in a separate directory and javac only moves .class files there.
The reason for this is that javac gives you more freedom in organizing your source files than most developers use. For instance javac does not care if your class is located in a folder structure that mimics declared packages. You can put a module together by putting several .java files along with some other files like .properties or .xml in the same folder. Your .java files can have different package declarations. For instance you can have files A.java:
package aaa.bbb;
class A {}
and B.java:
package zzz.uuu;
class B {}
If you ask javac to put classes in a target directory, it will create necessary subfolders for .class files, that will resemble the package structure. However it cannot do so for properties and xml files, because they do not contain package declarations. Such 'resource' management is left for the build tool.
Maven expects that you put such resources in another source folder (resources). They will be copied to generated package structure, so you should match it in resource folder. This is why it does not copy non-java files in source folders.
Eclipse expects you to put even .java files in a target package structure and complains if your package declaration does not reflect relative path of the file. This is where you have less freedom compared to bare javac. Thanks to this rule it can copy resources along with .class files and does not need another 'resource' folder.
You can configure Eclipse to use source folder as output folder. Eclipse will not touch resources in this case.
If you right click on the project in eclipse and select 'properties', then Java Build Path you see an input at the bottom for the Default Build Path, which should be target/classes. The source folders are shown in the main dialogue. If you click on the source folders then you can modify each, to exclude the xml files (if that is what you want to do).
Maven will include your xml files automatically if you put them in src/main/resources.
If you don't want to have xml files in build directory, you need to configure eclipse excluded source file types -
right-click on the file in the Project Explorer, choose Resource Configurations > Exclude from Build and choose the configurations that you want.

Use project's own .java files as resource files

I have made a small program in Java that displays its .java source with a gui. It does not use FileChooser to do this. I am reading the .java sources with the aid of following statements
String resName = "/dev/classes/"+name+".java"
Scanner s = new Scanner(FilePrinter.class.getResourceAsStream(resName));
where name is the name of the .java file i.e. if the file is MyProg.java then name==Myprog. Of course my program is inside the dev.classes package
The thing is that when I export my project to JAR and include source files this works because source files reside inside the /dev/classes/ directory.
However, I haven't yet discovered a way to make my program run in Eclipse or from the command line without giving me exception.
And of course when someone tries to add those source files to be used automatically as resource files the process fails.
Can I somehow use the same code both when running from Eclipse and from the JAR? It must be something very trivial but because I am not Java expert I cannot see.
I found how to do it. Actually you either have to use Ant or Maven. However, this can be done in Eclipse as well as follows:
On the Eclipse Project Properties>Java Build Path you can choose on the bottom Default Output folder: <your_project_name>/src.
This causes class files be compiled in the same directory as the .java files and finally does what I wanted.
Thanks to #AndrewThompson for suggesting to try this

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