Java compilation problem, Linux, project from Eclipse re-compile on Linux - java

I am trying to recompile an existing Java project exported from Eclipse. It is necessary to recompile this because I am running simulations remotely on other machines where a different (older) version of Java is installed. I have tried recompiling my .java file which specifies the simulation in question. However, it appears that it is necessary to recompile all other classes etc as well. Has anyone got an idea how to do this WITHOUT using Eclipse (I am not the Admin on the other machines and thus Eclipse is unavailable to me) and not manually because the project is quite huge?
Thanks a lot for any suggestions!

I recommend you to always have an command line way to build an application. The usual way to do this in Java is using ANT (or Maven).

As #Santiago Lezica says, Eclipse can generate an Ant file.
I believe that Eclipse allows you to build for an older target platform than the one you are currently running. That way you can do all of your builds locally.
The second approach has the advantage that you can fix any problems arising from compiling for the older platforms (e.g. use of new language features, use of new classes / methods) from the comfort of your own ... workstation.

There is another option that you should consider: Tell Eclipse to generate code for the old Java version (see the compiler options). That way, you can create code that runs on Java 1.3, even if Eclipse uses Java 5.

Not sure what your requirements are, but you could set the compiler level for your projects at the (older) level of your Linux installs. This would cause Eclipse to recompile it at that version, instead of a newer version.
At my company we use IBM's Rational Application Developer (instead of pure Eclipse), but I am assuming the option is in the same spot. If you right-click on your project, you can go to the Java Compiler options and then set the compatibility to the level of that on Linux (1.3, 1.4, etc.).
Since compile Java byte-code is supposed to be portable (for the most part), this should get you past most of your problems.
Otherwise, the other option is to use something like Ant or Maven scripts (which can be kicked off by Eclipse) and then just use a property to set the compiler right before you run it. This way you don't have to switch properties on your projects all the time, if you truly do need "newer" compiled code and can't live with "older" code on both systems.

Related

Another "Build path specifies execution environment JavaSE-13." problem

So, the full warning looks like
Build path specifies execution environment JavaSE-13. There are no JREs installed in the workspace that are strictly compatible with this environment.
I just installed JDK(version: 14.0.1 from here) and Eclipse(Version: 2020-03 (4.15.0) from here)
I've read post1 and post2 but i dont have any libraries in Java->Build Path -> User libraries. It's empty at all. In Installed Libraries i don't have any jdk{version}. I only have Name "JDK" and Location "D:\..."
Am i missing something vital? Because i have no idea how to fix it.
I am newbee, so if you have an answer or following question, please write a bit simpler. Thank you.
This should really be a comment, but I felt like spelling things out. The main thing to watch out for here is that with Java its version comes into play at least 4 times and there is nothing strictly stopping you from having 4 different versions configured on each occasion, which may or may not work. Add Eclipse and it becomes at least 5:
The java binary used to run Eclipse itself. (Configured via eclipse.ini)
This can affect how plugins behave. I remember a scenario where m2e would refuse to use a Java compliance level older than the version of the JRE running Eclipse. I can't make too confident specific calls from the top of my head here, but this is something to keep in mind.
Should be latest and greatest, though Java 8 appears to be the defensive option for obscure scenarios, such as projects depending on deprecated internal libraries. (tools.jar comes to mind)
The javac (java compiler) binary used to build your project. (Sits next to the java binary in a bin subdirectory of the JDK)
Should also be latest and greatest: every version of javac is capable of compiling source code written for previous versions as well as generating compatible byte code for older versions. This is what the following two points are about.
The Java version that javac is told to assert that your code is compatible with. (javac -source parameter)
The Java version that javac is told to make your build compatible with. (javac -target parameter; equal or greater than the value of -source)
The java binary used to run your build.
Should also be latest and greatest, though the minimum version required here is what was given to javac via -target, and incompatibility scenarios do exist.
The latest trend here is to ship every Java application with its own JRE to rule out problems arising from users having configured or installed Java in an incompatible manner. A user will then be required run a specially crafted native executable or a shortcut which will then invoke Java with the right arguments to start the application.
Now, you do not invoke javac or java even. In fact, the former is not even invoked by Eclipse. Eclipse actually comes with its own Java compiler, Eclipse Compiler for Java (ECJ). For what it's worth, we don't usually need to worry about that however. For Eclipse to know anything about available JREs, you need to match JDK locations on disk to Java versions "known" to Eclipse. This is done under Window -> Preferences -> Java -> Installed JREs -> Execution Environments.
After doing that, they should pop up in all the places where you can specify Java versions, which is mainly Project settings and possibly run configurations you have set up or Eclipse has set up for you. Project settings can then be set to up to infer the compliance level for building from the version of the JRE configured to run the build.
By the way, note that you probably want to use AdoptOpenJDK instead of the release from Oracle for reasons. You'll also want to just unpack any JREs which aren't the latest one in a nice folder somewhere on your hard drive instead of installing them. Installing them mostly means to make .jar files run with that version when launched from your OS's file browser. If you are on Linux, sdkman is currently the preferred way to do this.

Impact of Eclipse version on Code version

Does the version of Eclipse affect the version of code? Suppose, developers are using different version of Eclipse for same version of code with same version of JDK. Will it cause any issue?
No, the version of Eclipse does not matter.
Eclipse has extensive configuration for exactly which version of Java you are targeting. For example see the 'Java > Compiler' settings in the Preferences, and the 'Compiler Settings' in the Properties for a Java project.
You can also tell Eclipse which JRE/JDKs you have installed (see the 'Java > Installed JREs' page in the Preferences). You can run your code using any of these JREs, this is completely separate from the version of Java you are using the run Eclipse itself.
You do need to run a version of Eclipse which understands the version of Java you want to use. So for Java 13 you need to use at lease Eclipse 2019-12.
Of course newer versions of Eclipse contain more tools for helping with Java development and more bug fixes so using an up to date version is recommeneded.
Eclipse is simply an IDE (Integrated development environment). It combines debugging, compiling and other convenient tools for development. on the other hand, the Java code itself is separate, and you don't necessarily need eclipse to write the code. In fact, a simple text editor is enough. All you need is a file with a java extension (Example: hello.java).
It depends on what users share. I'm assuming you're using some sort of SCM, in which case the same project (fully shared) on different versions of the IDE can possibly lead to annoying situations.
There would be no issues in general, as far as producing the code is concerned, except in situations where some versions of IDEs don't support the required Java version (or other features). But I see this more as the user being affected rather than the project/code.
Here's what you should keep in mind:
Keep IDE project configuration files (such as .project, .settings, .classpath) ignored by your SCM system
All Java configuration should be based on a build tool. Use Maven/Gradle, etc, and any modern IDE will be able to create a project from its config file. The IDE would derive local configuration files which should not be sent back to shared repositories
This way, all developers are free to use whatever IDE (and whatever version) without causing interference, and CI tools will work off build tools' configuration as well.

How its possible for eclipse to compile and run programs on JRE without JDK [duplicate]

I need to batch a compilation with a special JRE which has been "customized".
Eclipse is able to compile the classes with this JRE, but I need to make a build script outside of Eclipse.
What is the method used by Eclipse to generate the .class files without a JDK?
Eclipse comes with its own compiler for the following reasons:
Incremental compilation (can compile just the changed parts of the project which can mean more than the amount of files you just saved, for example, when you changed some global)
The Eclipse compiler can create a class file even when the code contains errors. This allows to run the project even though not everything compiles.
The compiler provides Eclipse with an AST so it can do all kinds of fancy stuff (like the outline, show you all the places where the variable under the cursor is used, etc) at no extra cost (i.e. it doesn't have to run the compiler and another parser).
I believe Eclipse comes with internal compilers, and you can choose the compatibility to Java 1.3 through 1.6 (check the Preferences menu, under Java->Compiler). So Eclipse doesn't need an external JDK to compile, because it comes with it is self-sufficient.
If you want to create a build script outside of Eclipse, you're gonna need an external compiler, like the one that comes with the real JDK.
For the case one is interested: Eclipse's compiler is part of JDT core.
Eclipse was originally created by IBM. Eclipse has its own built-in Java compiler which is based on IBM's Java compiler, Jikes.

Can I develop Java programs if I have only JRE installed?

I have JRE 1.7 installed on my system. Due to some admin issues I don't have JDK on my system. is there any way I can develop Java programs with JRE only?
No you can't develop java programs only with JRE
You will need JDK for compiling your programs.JRE provides only runtime environment,but JDK is something you will need to compile your code to make them executable by your JRE.You will need javac for compiling your code which is present in JDK.
However for resolving the issue of admin rights you are having, you can download and install eclipse which has its own built in compiler.
You need some program which compiles your sources, but that doesn't neccessarily have to be the JDK.
For example you can download Eclipse which only requires a JRE, it has it's own compiler (built-in) and you can develop with that. Eclipse does not require any admin priviliges, it's a zip file which you can download and extract.
You need to compile them in order to develop and that is why you need jdk
You can't develop any Java application since you need JDK(Java Development Kit) to compile your code. JRE(Java Runtime Environment) provides run time environment only. You can run your compiled code in JRE.
the answer is somehow "yes,but..."
java source code execution procedure is split into 2 steps :
1)compiling the source code into a byte-code,thus generating the .class file
2)interpreting the VM specific instructions from the .class file into native instructions
.java----javac---->.class----java---->native code
needless to mention that the "javac" , the compiler of the java source code is in not part of the JRE!
so in order to develop java programs without JDK you have to skip the first step and keep using exclusively the "java" tool,thus dealing directly with byte-code !
in some extent and if you're acquainted with the JVM internals,it's possible to write some lines of byte-code using hex or text editors and then running the .class file;but you will not go further then that specially when it comes to creating complex applications as this require super-human capabilities :that's a highly daunting task .
nevertheless,todays java frameworks almost all use direct byte-code manipulation and tools like the asm framework or javassit
javassist does not require the JDK and you can generate a class from scratch and then compile/run it
java -classpath .:javassist-x.x.jar test "some arguments"
(no need to compile classes)
but again,that is only for restricted use and in order to develop java applications you have to use some JDK...
finally,you have to strive to acquire full control upon what is running in you machine;recent frameworks are resource-greedy and needs a huge amount of resources(disk space,memory...) and many tools and framworks require full control,i mean administrator right : for instance i remember that once a time borland delphi refused to start and i had to give it full access rights to start...
I request you to resolve admin rights. Else I have two options for you.
1) You cannot do anything without Java Development Kit.
2)You can install a IDE to code Java which makes your work in the current situation simpler. Use Eclipse or NetBeans. I would recommend Eclipse to you anyway. You can download eclipse from this link https://www.eclipse.org/downloads/
Try downloading JDK from this http://www.oracle.com/technetwork/java/javase/overview/index-jsp-138218.html.
Hope this helps you.
JDK is needed to compile your java file into class file. And JRE will execute the generated class file and provides the output.
If you have admin problems in your m/c then you can install the JDK in some other m/c and take the class files into your system and execute them.
Eclipse includes its own compiler and doesn't depend on the JDK compiler. But indeed, the JRE is just the runtime environment and doesn't include the compiler or other development tools.
Do you need this to do you job or not? If it's your job, you should have access or they should install it. If you don't have access because it's not your job, I suggest installing the JDK at home (or on a laptop you can take with you)
Due to some admin issues I don't have JDK on my system.
Install it in your home directory or another directory you can write to. Your admins can't prevent you from installing the JDK if you have enough disk space.
JRE is not just sufficient, without compiling the code for that you need Jdk which have compiler to compile the code.

Compile complex Java Program in native code using GCJ

I have a java program consists of a jar for the "real program" and a sub-folder containing 20 jar library files (also have dependencies between them). I tried but without success ... I'm missing with the many options that gcj to compile :(. Does anyone know how to compile to native code so that a java program?
Thanks
Update: I have used the plugin for Eclipse (I'm using the 3.6 version of Eclipse and the plugin works well) http://gcjbuilder.sourceforge.net/ to generate a Makefile for the compilation. But now I have an another problem :(
Some libraries are not compiled (exit for error). The error is that GCJ considers all .class files inside .jar file, that have dependencies with other libraries do not exist. But for the purposes of my program, these classes are not used (that is, the java program it works properly). There is thus a way to force gcj to consider only the classes actually used in my java program?
I can only recommend that you don't use GCJ. It isn't Java. The compatiblity matrix on their web page is far from encouraging. It doesn't implement Java 1.2 completely yet, let alone all of 1.3, 1.4, 1.5, 1.6. The project appears to be falling further and further behind, and it never had 100% Java compatibility as a stated goal in the first place.
You may want to look at tools like autojar and firends to help resolve your dependencies and to make a single jar.

Categories