How to edit library source files on NetBeans? - java

I am doing JAVA project based on the SSJ (Stochastic Simulation in Java) libraries. In my project I have followed the required steps to import the SSJ library files and also the source files.
When looking at the SSJ library files in the project's Libraries branch, only class files appear shown, but when I open them I can view the source code (which I couldn't before, as I hadn't imported the sources) so I assume everything was imported correctly.
When I run the examples provided with the library everything runs fine and as expected. But if I try to change the source code for the libraries for instance, changing the text on an output, the changes I make to the source files aren't reflected in the output from the java programs.
Any hint on what I might be doing wrong?
I understand this might be hard to understand or test as I can't quite create and SSCCE for this matter. The library files are hosted here (http://www.iro.umontreal.ca/~simardr/ssj/ssj-20120216.zip) and the source code here (http://www.iro.umontreal.ca/~simardr/ssj/ssj-source.html).
Thanks in advance for all your help!

Adding source to libraries it's for debugging and documentation purposes. It does not mean that Netbeans will detect changes and recompile classes automatically.
You should create a project for the library itself to recreate it from source. Changes are that if you have the source there will be a build.xmlor a pom.xml that contains the steps to compile the library and will allow to create a Java Free Form Project or a Project with existing POM.
You'd then remove the reference to library itself and add a dependency to the project.
Other option would be to add the library source to the same project you are using (and, again, removing reference to the library) but I would not recommend this unless it's a very small simple library.
EDIT. Particular ssj information.
ssj is a library where the original source code is in .tex files and it requires several programs available to generate .java files and then compile them. All this information can be found in the COMPILE.txt original file.
Since the source distribution includes .java files it is possible to generate a .jar just using Netbeans but it requires to modify the build.xml to just compile Java source files. Also when creating the Java Free Form Project is necessary to select lib as the target to Build Project.
Here it's a minimal build xml for Netbeans 7.1 to compile from Java sources.

In order to create the new *.java file from the *.tex file, you need the tcode.jar which is available in the distribution. Have you added tcode.jar as an external jar to your project?
umontreal.iro.lecuyer.tcode.Texjava is in tcode.jar

Related

See what's inside classes we import from jar files

Started coding in java , just wanted to see the implementation of classes and interfaces we actually import to use their methods.
Ex:
import java.util.Scanner;
I am curious to see what's inside the class Scanner(){}
Is that possible?
Tried Searching but couldn't find the implementation and definitely did not understand how they hide them from public.
Please follow the following Steps to See Java predefined classes implementation
1) Get rt.jar (It contains all Core Java API Classes which required for JRE at Runtime)
Download it from the internet directly or Search it in JDK at Path like C:\Program Files\Java\jdk1.8.0_181\jre\lib and copy it to other location on your Desktop
2) Extract the copy of rt.jar
Extracting .jar file with command line
https://www.wikihow.com/Extract-a-JAR-File
After extracting you can see all the Core Java Classes in .class file (cant read it Directly) which we use normally in our programs
3) Download tools like cavaj java decompiler to open and read .class file
You Can see any code in Any jar with this approach... Its very smart way to understand basics of java with fun ..
There is nothing "hidden from public" concerning java.util.Scanner.
#shmosel already linked to the source of it, #Stultuske pointed out, that there are various websites covering source code of java.
Modern IDEs also allow you to view into source for your libraries (aka .jar files).
My Netbeans IDE shows source from known jar files whenever I ctrl-click on a class.
Same goes with Eclipse, IntelliJ, ...
For a lot of open-source libraries (e.g. from Apache) there is also source code available. Just by cloning the version control repository, downloading source packages (zip, ...-source.jar) or browsing the version control repository online.
If project is buildable with maven, the IDE can even download and show sources for open source project automatically.

Converting android library project to jar file

I have a create an android library project which has to be given to a 3rd party application,I have two requirements,
1)convert the library project into a jar file (or),
2)Make the source code in the library project obfuscated.
Basically i dont want to 3rd party application developer to get my source code.
Please help! thanks in advance!
The ProGuard-Application lets you shrink and obfuscate your code. It is from the Android developers, so you can assume it is safe to use it.
This is the closest that you can get:
1: Create a regular Android library project, and get it working.
2: Copy that Android library project into another directory.
3: Create a JAR from the compiled Java classes from the original Android library project, and put that JAR in the libs/ directory of the copy you made in Step #2. You should be able to run ProGuard on this JAR manually, though I haven't tried that.
4: Remove everything inside the src/ directory of the copied library project, leaving behind and empty src/ directory.
5: ZIP up or otherwise distribute the copied Android library project.
This is what you need to do:
Create your library project. Write a test application that tests your library. Build your library project. You could use ant/maven through eclipse. Most libraries now need gradle support because applications are now using Android Studio. Mark your project as a library by going to Project -> Properties -> Android and select the Is Library checkbox. Check this link: http://developer.android.com/tools/projects/projects-eclipse.html.
If you don't want a 3rd party app developer to see your source files, you need to enable proguard. This tool shrinks, obfuscates your code. The application that uses your library has a lot of control over what code needs obfuscation. For example, an application can decide that it wants all packages starting with com.blah.blah* to not be obfuscated by specifying the -keep option in its proguard config file. This will prevent certain sections of code from getting obfuscated. You should allow default obfuscation for libraries unless you decide to use components which don't function with obfuscation (like annotations or reflections).
By enabling proguard, a third party developer will not get access to your source on reverse-engineering your apk. Use wisely!

Linking to generated Java protobuf code in Eclipse

Here's the workflow that I'm trying to build
compile my proto files using a script, putting the generated src in a specified directory
link to the generated classes in Eclipse
compile my project
I am easily able to do this for C++ using Eclipse CDT: In my project I choose File->New->Other and choose File under General. Then I click on Advanced and select link to system file.
I haven't discovered how to do this for Java, though. Once answer here suggests creating another project with the generated code and make that a dependency to my project. This works but seems redundant.
Is there a way to directly link the generated protobuf Java classes to my project?
Well Java doesn't really have a "link" phase. Your two options are really:
Build the generated code outside Eclipse, and then add a reference to the relevant directory or jar file
Include the generated code within Eclipse (e.g. by having a source path which includes the directory containing your generated code) and get Eclipse to build it along with the rest of your code.
I suspect that the first option will make it easier to keep the generated code well away from your real source, but the second option may make it easier to browse the generated source, and package everything up.

Java JNA does not seem to be working

I recently downloaded the Java JNA from https://github.com/twall/jna/servlets/ProjectDocumentList?folderID=7408&expandFolder=7408&folderID=0 and tried using it. However, as exemplified by the screenshot below, the demo source code provided in https://github.com/twall/jna/ won't compile, no matter where I place the files. Mind you, I've tried placing them both in the default package an every other combination of package mix-ups, and this is only the latest version. Does anybody know why the compiler can't find what is contained within the JAR file? (Also, as you can see, the class files are all in the jar file to be found)
Basically, the JAR should go in the classpath. In Netbeans, you can do it by adding it as library to the build path of the project through the project properties.

How do you use a Java Library?

I'm trying to use an open source java library to visualize nodes and edges in a graph, but I'm completely lost.
I have a bunch of jar files in a folder. Clicking on some of the jar files makes java swing windows pop open with graphs displayed. Clicking other jar files does nothing.
If I figured that out, would I just stick the jar files in there with the other ones, or would that still not work?
And if I ever figure out how to use these files, does that mean that I have to include them if I transfer my java project to another computer? How would I go about doing that?
I believe if you put the jars in your classpath, you can import and use classes just like you would a standard library. Figuring out the classpath can be confusing, but you can just set it when you start your jvm. Your IDE may have options for it, too.
Most java problems are classpath problems.
Have you included those libraries in your classpath?
If you are using eclipse, you could
Project - > properties -> Java build path ->addJar.
And the Jar file should be placed in a directory inside your workspace (lib/ for example)
If you have to take your project to another computer, you could take these steps
Before doing anything, export your project (as a Jar file, for example).
Save it into your favorite drive (cd / usb drive/ diskette/ tape).
On "the other" computer, you can import this project into your workspace
In Eclipse, you need to add libraries to the project build path.
In general, you need to provide dependencies via the classpath mechanisms at compile time and runtime. The precise mechanisms vary, but, for example, if you used the javac compiler, you would provide your libraries on the command line:
javac -classpath C:\dir\lib1.jar;C:\dir\lib2.jar foo/MyClass.java
These dependencies would also be required to invoke the app:
java -classpath C:\dir\lib1.jar;C:\dir\lib2.jar;. foo.MyClass
This page gives some good info, though googling for the term "classpath" should provide alternative sources.
You use it by including it in the classpath of your java application, that way you can reference it from your code. Here is a starter document. The JDK 1.6 has some easier options (such as specifying multiple jar files as *.jar). It is definitely a little complicated, but it is very worth knowing.
You should have documentation for these Jars. Some sounds like examples, but one must be the core graph modelling and rendering Jar. Hopefully the examples have source included.
Just add that Jar to your project in Eclipse (e.g., in a /lib folder in your project, then add it to the build path) and use the documentation to use the code. You can also use Eclipse to look inside the Jar file.
Unless there is no alternative, it probably isn't worth using a load of third party code that isn't documented at least on the API level, and without any source examples definitely not.

Categories