Linking to generated Java protobuf code in Eclipse - java

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.

Related

VSCode doesn't automatically generate .class file for java

I'm new to java and learned that when creating a .java file usually there's a .class file generated automatically, which happened to the previous java files I created.
However, I forgot since when VSCode stops doing this when I create new java file.
Another problem is, when creating a new java file, the shortcut to type "main" and press enter doesn't generate
public static void main(String[] args) {
}
anymore. I have to literally type out the whole thing, otherwise I have to close this new file, open again, wait a few seconds to half a min or so for the shortcut to work.
Any reason why?
The .class file is generated by compiling the .java file. The following settings in settings.json control the generation of .class files in the bin directory.
"java.project.outputPath": "bin",
In addition, you need to download the Extension Pack for Java, read the official document for more help.
Also check the following settings to control the location of code snippet suggestions.
"editor.snippetSuggestions": "inline",
Sounds like you've used some sort of IDE before, maybe IntelliJ or Eclipse.
The .class files
The .class files are compiled Java source files, containing JVM bytecode. These are generated when you build your Java program, either via a build tool (Maven, Gradle, Ant, etc..) or by compiling the sources. Now, if you use an IDE in most cases the IDE will take care of building your project. If you use the stock VSCode without any Java related plugins, VSCode doesn't know how to build a Java project out of the box. I believe you can define a build task, and run that, but it doesn't support it out-of-the-box, without any plugins. So you should look around in the VSCode plugin marketplace what Java-experience-enhancing plugins you can add.
Code snippets and shortcuts
Not sure why you have to reopen files for shortcuts to work. That being said, you're looking for code snippets, or IIRC IntelliJ calls these live-templates. These are, well, templates for code generation, which you can invoke in your editor. IIRC VSCode doesn't have any Java related code snippets, you have to add them yourself or install a plugin that provides these. In IntelliJ, you have built in templates or snippets for stuff like the main function, for-each blocks, etc.. but again, IntelliJ is a JVM-focused IDE, a very good one too. VSCode is a really good tool, but you may have to install some plugins and add stuff in order to have the cosy IDE-like experience.

How to edit library source files on NetBeans?

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

Integration of Java and Python Code in One Eclipse Project

I am writing a compiler in Python using Eclipse with PyDev. I've come to a stage where I needed to write some code in Java. I'm wandering if there is a way of combining these into a single project, because at the moment I have two separate projects, and whenever I need to change the Java code, I have to manually copy the .class file into the Python project.
If this is not possible, what would you suggest is the most elegant way of structuring the files of these projects, and how should I set up my build process?
Thanks.
You can keep things in separate projects without having to copy the .class files to the Python project provided that you make that project a PyDev project and add the place that contains the .class files (i.e.: bin folder) as a source folder in the PyDev configuration (and reference that project from your Python project).
Take a look at "Project reference for Jython users" (in the end of http://pydev.org/manual_101_project_conf2.html).
I think you could also do what you asked in your question: create a java project, set it as a PyDev project too (right click that project > pydev > set as pydev project) and configure the PyDev project properties setting the PYTHONPATH to the place where you have your Python files and add another entry to the bin folder (i.e.: where the .class files exist). Not sure if it'd be better to keep those as 2 separated projects though.
I think Jython would be ideal here, as the Python code essentially gets translated to Java bytecode and run on the Java virtual machine, making it easy to communicate between the two. Also, pydev itself integrates well with Jython.

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.

Multiple IDE debugging in Eclipse

First off I'm not a Java expert by any stretch, nor am I a regular Eclipse user. Due to personal circumstances of a member of our team I have had to step into the breach and fix some bugs in two java applications currently under development.
We have a core application (CORE) and an API which includes a reference to that CORE. What I need to do is debug the API but step into the CORE code. I have all the source code for both projects loaded into the IDE but I can't hit breakpoints in the CORE. I'm assuming it is using the jar version of the CORE included in the API as opposed to the code in the CORE.
I hope that makes sense.
Cheers
Define a breakpoint in the API by clicking on the left side column of the editor. Then, try to step into a function of CORE. Eclipse will complain that it cannot find the sources to debug. However, a button will appear that will let you define where to find these sources: either in a jar file or in an external directory.
If you were to include the sources of CORE along with the classes of CORE, normally eclipse will find it automatically as well.
Cheers
I have all the source code for both projects loaded into the IDE but I can't hit breakpoints in the CORE. I'm assuming it is using the jar version of the CORE included in the API as opposed to the code in the CORE.
Change the build path of the second project so that instead of referring to a packaged JAR of the first project, it refers to the project itself.
In the properties of the second project, go to Java Build Path, remove the JAR from the Libraries tab, and add the first project under the Projects tab.
What you need to do is remote debugging. Here is one (of several) articles covering that topic and showing how to set up a remote debugging session. (near to the end of the text)
Basically you start the application with some extra parameters while on eclipse you have a projects that contains the source code. Then you start a remote debugging session and can set breakpoint, inspect variables, etc. on the remote JVM.
You probably have the CORE java classes which are compiled by Eclipse to byte code and present in the IDE. THat means that the code invoked by the API is most likely NOT the code you think as it is probably present in jar files next to the API.
What you need to to is to attach the sources to the jar files instead in their properties. Either zip them up and attach that zipfile or attach the folder where the "com"/"org" etc package names start.

Categories