Linking sourcefiles to compiled files within Eclipse - java

I'm trying to build a jar from an existing project. Since I want to be able to lookup the sourcecode within Eclipse (debugging etc.), I need to link the source files. I compiled the sources with this expression:
javac -d classes/ -s src/ -verbose -g -sourcepath Framework/ src/DivaAPI.java src/DivaCallBase.java
Afterwards I run this commands:
cd classes
jar -cvmf manifest.txt Framework.jar package/name/*.class > nul
My directories look like this:
root
src
.java files
classes
package/name/.class files
manifest.txt
Afterwards, the generated jar-file is copied to the lib ordner within my project, import it in the library tab (properties/Java Build Path/Library ) and almost everything works fine. Except the source code viewing of the imported library. I can't figure out how to link my existing sources in Eclipse. I know about the "attach source" button. But if I click it and select the correct source folder, Eclipse tells me that the folder doesn't contain any source files.
It seems to me that I do something wrong, but I just can't figure out, what it would be.

Ok, I needed to search just a little bit more. With the help from this post ( First answere and second comment of that answere, all by #Ed Staub ), I figured it out.
The easiest way is to generate a new jar, but this time with java files. Name it and copy it to the lib folder. Use the "attach source" button again and navigate to the lib folder and select the source.jar.
Now, everything should work the right way.

Related

Java/VScode- .Class Files Not Being Sent to Output Directory

I'm using VSCode for my IDE for Java. I have the Setting.json file configured to output my .class files to a directory called 'build':
"java.project.outputPath": "build"
My Project Folder/File Layout is as follows:
math
\src
Main.java
\lib
Math.java
\build <- This is where I want my .class files to be built keeping the same file structure as
in the src folder.
When I use
javac src\Main.java
The compiler writes the .class files in the .\src directory, but I'm telling it to write the output in the .\build directory. Any ideas what's going on?
I didn't see settings.json in the workspace directory structure you provided. I think this may be the cause of the problem.
"java.project.outputPath"
This setting only takes effect in the workspace scope.
You can create a folder named .vscode in your workspace, then create settings.json:
Add the codes in workspace settings.json: "java.project.outputPath": "build".
I fixed the problem. I was using the Command Line. VSCode only builds to the specified build path if the build command is run through the gui.

Finding .class file in Eclipse

I need to turn in the .class file of a class from Eclipse. I managed to turn in the .java version of the file by clicking and dragging it into my browser, but I'm not sure where to find a .class file. I've read that I can find it in the "bin" but I have no idea where this is.
Thanks for all the help!
Generally, Eclipse projects have the following file system:
Proj/
.classpath
.project
src/
Foo.java
bin/
Foo.class
So, navigate to the Project folder, go to bin, and your classes should be right there.
If you are on a mac:
If you are in your project in the command line (you have done cd project)
You can do
cd bin
open .
And your class files should show up in a place that you can drag them into a browser. I don't understand why you need to submit .class files, as they are generated when the code is compiled.
Ctrl + Shift + T
will list down all the files including .class files in all of your projects in the workspace.

Extract source code from .jar file

Is there a way to extract the source code from an executable .jar file (Java ME)?
Use JD GUI. Open the application, drag and drop your JAR file into it.
You can extract a jar file with the command :
jar xf filename.jar
References : Oracle's JAR documentation
I believe this can be done very easily. You can always extract the source files (Java files) of a jar file into a zip.
Steps to get sources of a jar file as a zip :
Download JAD from http://techieme.in/resources-needed/ and save it at any
location on your system.
Drag and drop the jar for which you want the sources on the JAD.
3 JAD UI will open with all the package structure in a tree format.
Click on File menu and select save jar sources.
It will save the sources as a zip with the same name as the jar.
Hope this helps.
The link is dead due to some reason so adding the link from where you can download the JDGUI
Your JAR may contain source and javadoc, in which case you can simply use jar xf my.jar to extract them.
Otherwise you can use a decompiler as mentioned in adarshr's answer:
Use JD GUI. Open the application, drag
and drop your JAR file into it.
I know it's an old question Still thought it would help someone
1) Go to your jar file's folder.
2) change it's extension to .zip.
3) You are good to go and can easily extract it by just double clicking it.
Note: I tested this in MAC, it works. Hopefully it will work on windows too.
Do the following on your linux box where java works (if u like the terminal way of doing things)
cd ~
mkdir decompiled_code && cd decompiled_code
// download jar procyon from https://drive.google.com/file/d/1yC2gJhmLoyE8royCph5dLEncgkNZXj58/view?usp=sharing
java -jar procyon-decompiler-0.5.36.jar /Path/to/your/jar -o .
NOTE : as #Richard commented "this may be illegal depending on whether you own the copyright to the jar, the country you live in and your purpose for doing it."
Steps to get sources of a jar file as a zip :
Download JD-GUI from http://java-decompiler.github.io/ and save it
at any location on your system.
Drag and drop the jar or open .jar file for which you want the
sources on the JD.
Java Decompiler will open with all the package structure in a tree
format.
Click on File menu and select save jar sources. It will save
the sources as a zip with the same name as the jar.
Example:-
We can use Eclipse IDE for Java EE Developers as well for update/extract code if require.
From eclipse chose Import Jar and then select jar which you need. Follow instruction as per image below
AndroChef Java Decompiler produces very good code that you can use directly in your projects...
Above tools extract the jar. Also there are certain other tools and commands to extract the jar.
But AFAIK you cant get the java code in case code has been obfuscated.
suppose your JAR file is in C:\Documents and Settings\mmeher\Desktop\jar and the JAR file name is xx.jar, then write the below two commands in command prompt:
1> cd C:\Documents and Settings\mmeher\Desktop\jar
2> jar xf xx.jar
-Covert .jar file to .zip (In windows just change the extension)
-Unzip the .zip folder
-You will get complete .java files

Using javah -jni with an Eclipse project structure

I need to know if I'm doing the things in a wrong way.
I have the following project structure (a pretty standard one):
then I've configured javah as external tool like this:
When I run the external tool on OSManager4Windows.java I was expecting to find it_univpm_quickbackup_utils_OSManager4Windows.h in bin/it/univpm/quickbackup/utils/ but it is inside bin. Is that correct? Shouldn't be inside the same directory of the .class file?
The problem is that javah generates the header file on the directory that the command was executaded (which you specified the bin folder). It makes sense to generate all headers in the same folder because usually c/c++ project put all of headers in the same folder.
However, if you want specific folders, the following command generate the headers in the specific src folder.
-d "${workspace_loc}${system_property:file.separator}${container_path}" ${java_type_name}

making a .jar file with source and project structure?

I wonder if this is possible with ant and java:
I have a Java project which looks like this:
./
build.xml
src/
com/
example/
{.java files here}
bin/
com/
example/
{compiled .class files here}
lib/
{3rd-party jar files here}
dist/
{jar file(s) here}
I know how to make a .jar file that contains the stuff in the bin/ directory, with the right manifest to make it run my main Java class.
What I would like to do, if possible, is to make a .jar file that:
Java can execute ("java -jar myproject.jar")
Someone else can unzip to create the project structure. (including all the subdirectories except for the "dist/" directory)
Is this possible? I can't seem to tell Java to use the bin/ subdirectory of the .jar file as the classpath, I may be screwing up the syntax somehow.
edit: Okay, it sounds like trying to make one object serve two (too many) purposes. I have abandoned this approach, instead creating a standard .jar file as the executable, and a .zip file with the source (the build.xml + the src and lib directories). That way there are 2 easy downloads, more than 1 file but not too bad.
No, UrlClassLoader always tries to find classes based in the root, and a jar URL will always fetch entries based in the root of the jar file.
You could create a jar file which has binaries from the root, but source files under src etc. That wouldn't be too bad, assuming you really do just have com as the only "root" package.
You could put your project on a public SVN server, and just include instructions on how to check out the source - you'd also benefit from other people's check-ins and improvements (hopefully not vandalism).

Categories

Resources