views:

94

answers:

3

I've wrote a java code and compiled it. (foo1.6.class) According to my search, my local machine has Java 1.6, and the tomcat server that I uploaded foo1.6.class only accepts version number 1.5 This means that I have to have Java 1.5 to compile?

I believe it is the cause that bad version number error is thrown like below.

My question is, is there any way I can compile my Java file using 1.5 version number? Looked at javac cmd but seems like it is not part of the option. But I don't think remove 1.6 and install Java 1.5 for this reason is not really good option neither. How do people deal with this kind of situation?

Thanks in advance!

exception

javax.servlet.ServletException: Bad version number in .class file (unable to load class resume_builder.ResumeBuilder)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:273)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class resume_builder.ResumeBuilder)
    org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1884)
    org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:889)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1353)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1232)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:128)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
    java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    java.lang.Class.getDeclaredConstructors0(Native Method)
    java.lang.Class.privateGetDeclaredConstructors(Class.java:2357)
    java.lang.Class.getConstructor0(Class.java:2671)
    java.lang.Class.newInstance0(Class.java:321)
    java.lang.Class.newInstance(Class.java:303)
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:142)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
+10  A: 

You can use javac -target 1.5 -source 1.5.

The Java compiler allows you to use a lower than it's current target version number only if you also use a source compatibility version that is not higher than the target (there are execptions for source versions below 1.4).

x4u
Thanks guys! I ran:javac -cp . -target 1.5 -source 1.5 /dir1/foo.java and it successfully complied. Uploaded to webserver and waiting for the effect to take.....
masato-san
-1 This is not safe. See the question this is effectively a duplicate of (which I am about to find and post as a comment above).
Kevin Bourrillion
+4  A: 
javac -help
Usage: javac <options> <source files>
where possible options include:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings
  -verbose                   Output messages about what the compiler is doing
  -deprecation               Output source locations where deprecated APIs are used
  -classpath <path>          Specify where to find user class files and annotation processors
  -cp <path>                 Specify where to find user class files and annotation processors
  -sourcepath <path>         Specify where to find input source files
  -bootclasspath <path>      Override location of bootstrap class files
  -extdirs <dirs>            Override location of installed extensions
  -endorseddirs <dirs>       Override location of endorsed standards path
  -proc:{none,only}          Control whether annotation processing and/or compilation is done.
  -processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery process
  -processorpath <path>      Specify where to find annotation processors
  -d <directory>             Specify where to place generated class files
  -s <directory>             Specify where to place generated source files
  -implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files 
  -encoding <encoding>       Specify character encoding used by source files
  -source <release>          Provide source compatibility with specified release
  -target <release>          Generate class files for specific VM version
  -version                   Version information
  -help                      Print a synopsis of standard options
  -Akey[=value]              Options to pass to annotation processors
  -X                         Print a synopsis of nonstandard options
  -J<flag>                   Pass <flag> directly to the runtime system

Note the -source and -target arguments?

Paul Tomblin
It's possible to be polite even when telling someone they should have checked the -help option. This isn't it.
DJClayworth
If you think that's me being impolite, @DJ, you might want to cover your eyes when you see what really counts as impolite on the internet.
Paul Tomblin
I actually think this is quite helpful. The first line is a nice "how to" for anyone who doesn't know it's possible. No snide or snarky comments. Only the thinnest skinned person would take offense at this.
duffymo
@duffy, it appears that DJ has now gone on a snit and is downvoting my answers all over the place. Fortunately I can afford to lose 2 points at a time for a lot longer than he can afford to lose 1.
Paul Tomblin
How mature of him/her. It's still disheartening.
duffymo
Just checked DJClayworth's nic. I would have expected more from a 47 year old who claimed to be a team lead. This kind of behavior must make him/her a joy to work for. Fortunately he could spend his/her entire rep dissing you and you'd still be left with a healthy (deserved) rep when s/he hit zero again.
duffymo
I didn't downvote this answer. I haven't downvoted anything in weeks. I have no idea what you guys are talking about.
DJClayworth
+1  A: 

The right answer in your situation is the -source and -target flags as pointed out by x4u.

You ask about needing to uninstall Java 6 to be able to install Java 5. This is rarely necessary. The "javac" compiler comes with the JDK, and it is possible to have many of these installed. You may consider installing the Java 5 JDK without uninstalling Java 6, and then just compile with javac from that installation. This will ensure that your code will run with a Java 5 runtime (and is what I personally do).

Thorbjørn Ravn Andersen