Java newbie (linux,Terminal) level is 1.5 - java

I am trying to Build a JAR File of Interface Classes
I am using Linux and working throughout the terminal
I keep getting this error
Syntax error , type parameters are only available if source level is 1.5
I searched many sites and most solutions were about java on windows
to change properties
but how can I do that on linux ?
how can I solve this issue please ?

In your javac command line, add "-source 1.5".
It should read:
javac -source 1.5 ...
See also:
http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html
http://docs.oracle.com/javase/1.5.0/docs/tooldocs/solaris/javac.html

Check your java version in your command line using:
java -version
and be sure that you are using a version 1.5 or more. And check that in your build.xml you are not saying that your java version is less than 1.5.

It can be fixed in same way as on windows.
If you are using Ant to build your project just add or change source attribute of the javac task, see Ant javac documentation. It should be looks like:
<javac ...
source="1.5"/>
If you are using command line tool to perform compilation ensure that you are using the correct version of the java compiler (see output of the java -version, you can't compile java15 java source classes using 1.4 or lower java compiler) and add -source 1.5 parameter to compile command. It should be like:
javac -source 1.5 ...java classes...

You have Java source code that uses generics, which is a feature added to Java 5, but you are most likely using a Java compiler from an older version (Java 1.4 or older). You can check which version of Java you are running with a command like java -version.
Install a newer JDK. What the best way is to do that depends on your Linux distribution.

Related

javah missing after JDK install

I can find java, javac and javadoc but there is no javah.exe in my jdk\\bin folder.
I tried to reinstall the JDK but it is still missing. How can I get it, why is it missing?
I found a similar question where the operating system was Linux but I can not find answers for Windows users.
My OS is Windows 10. The Java version is 10.0.1.
The tool javah.exe was removed in Java 10. The reason is simple, it is obsolete. From JEP 313: Remove the Native-header Generation Tool (javah):
Motivation
The tool has been superseded by superior functionality in javac, added in JDK 8 (JDK-7150368). This functionality provides the ability to write native header files at the time that Java source code is compiled, thereby eliminating the need for a separate tool.
Focusing on the support provided by javac eliminates the need to upgrade javah to support recent new paradigms, such as API access via the Compiler API in javax.tools.*, or the new java.util.spi.ToolProvider SPI added in JDK 9.
So you can just use javac.exe if you are on Java 8 or newer.
javah has been superseded by the -h option added to javac in JDK 8.
It is deprecated since Java 9.
See here for details.

Installing JDK 1.2 in Windows 7

I want to recompile an old jar file (which was compiled in java 1.2). So that there are no errors i need to compile it in Java 1.2 aswell. But havent found a jdk 1.2 which i can install on windows 7 (and 64bit).
Any suggestions?
thanks in advance!
Yes, you can set the version of compiler at compile time. And compile your java code into old versions of java.
From Oracle article : http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html
Cross-Compilation Example
Here we use javac to compile code that will run on a 1.4 VM.
% javac -target 1.2 -bootclasspath jdk1.2/lib/classes.zip \
-extdirs "" OldCode.java
There are two scenarios, just compiling old code and actually developing for an old JRE.
For just compiling you don't need an old JDK, you can adjust the target language level javac compiles with the -target option (see: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html) - Although there may be edge cases that may break the compiled code if the compiler decides to select a different overload for a method that does not exist in the old JRE.
For developing old JRE compatible code, you could use above method but you run the risk accidentially using an API that isn't present in the real old JRE. To avoid that problem you need the actual 1.2 API, meaning you need the rt.jar file from a real 1.2 JRE/JDK. This can be added into your project in your IDE (and the current e.g. 1.8 JDK removed). The detailed procedure how to set this up depends on the IDE. Likewise the 1.2 rt.jar can be provided to javac, also using command line switches. Again you need no runnable 1.2 JRE to compile/develop.

Regarding cross complitaion in java

I have my project on which is need to be build on jdk 1.5 and rite now I am using jdk 1.7
can u please advise the command line parameters that I should add so that myy java project
to compile and build on jdk 1,5 itself still i am using jdk 1.7 what i have tried is ...
javac -source 1.5 -target 1.7
I am getting this error below..
[Step 1/2] Ant output
[12:52:04][Ant output] Error: Could not find or load main class javac
what my basic concern is that i have my teamcity server is runnig on jdk 1.7 and my project should be build on jdk 1.5 so for that in teamcity we can specify external command line parameters for code to run explicitly on jdk .1.5 , Please advise
If you have the Java 5.0 javac then you can only build Java 5.0 source code. Once compiled this code can run on Java 5.0 or Java 7 or Java 9
I suspect the default command line options are what you want, and if not you will have to upgrade your JDK.

Ant using a lower version of Java than the system's Java version

This behaviour appeared strange me. The following is what happened:
We have a build.xml file for Ant, which says <javac source="1.5" target="1.5"></javac>.
We have updated Java on our system (Linux) to 1.6, but we forgot to update the Ant build.xml file.
Ant building is happening successfully, How is it happening? Is it like the higher version of Java can downgrade to lower versions? Or if Ant cannot find the version of Java specified it uses the default version?
There will not be any problem in upgrading JDK in this scenario
<javac source="1.5" target="1.5"></javac>
Means that it tells Java compiler that source is written using Java 1.5 and it should create the target build suitable for Java SE 1.5 environment.This is really possible with newer JDK.This is exactly the same as
javac -source 1.5 -target 1.5
Please refer the documentation of the javac tool
Hope this helps
These options are passed on to the Java compiler. It can generate byte code for older JVMs (target) and it can also accept source written for older Java versions (source). This makes for example sense, when a new Java version introduces new keywords but you use that word as identifier in your source.
It will generate the byte code of JDK 1.5 instead of JDK 1.6, because Java supports backward compatibility so byte code of JDK 1.5 could easily run on JDK 1.6 JRE.

How to compile and run lower version java to higher version?

I am currently working in jdk 1.5 version. I want to compile and execute code in jdk 1.6 version.
How can i do this?
It's a bit unclear what you're asking.
If you're asking whether you can compile code with JDK 1.5 and run it with JDK 1.6, yes, that's fine. 1.6 can run code compiled with 1.5 without trouble. You won't be able to make use of any of the Java 6 (1.6) compile-time or API enhancements, of course. To do that, you'd have to compile with the 1.6 (or higher) JDK.
If you're asking how to start using 1.6, just install it and start using it. (That's a very general and somewhat vague answer, but then, it's a very general and vague question.)
Use the -target and -source flags to tell the compiler what source version you are using or what target version you plan to run it on
javac -target 1.6 -source 1.5 [classes to compile etc]
http://docs.oracle.com/javase/6/docs/technotes/tools/solaris/javac.html#options
Install JDK 1.6 and re-set your JAVA_HOME variable. Normally, the code from the older platform versions will work on newer ones.
if you are using an IDE, there must be an option with the location of java compiler, change to the directory of your installation of your new jdk.
else, change the value of your environement variable JAVA_HOME to the directory of your installation of your new jdk.
you don't have any change to do in your code
jdk 1.6 supports all features present in jdk 1.5.So u can compile and run your program on jdk1.6 without any problem.
Java is backwards compatible, so you can compile and run your application on 1.6 without any problems but maybe with some warning messages on compile.
You can use also the arguments -target and -source. More info here
Have a look at the java documentation about compatibility issues.
Find here

Categories

Resources