Have jre7; JAR is jre6 compiled; can't run - netbeans

I built a JAR in NetBeans, setting the Source/Binary Format to JDK6. So it's compiled to jre6. I can use the JAR (outside the IDE), but if I send it to a machine with jre7, I have to alter the environment variable to get it to execute from the JAR.
Isn't Java backwards compatible? Shouldn't this work without effort on jre7? I am not doing -anything- fancy in this program. It's a few kilobytes, couple hundred lines, import on some Map classes from java.util.
Is there a command-line option for java to use an older runtime, or an annotation to write in, to identify the class files as version 6?
Embarrassed I can't find specs to answer these questions.

Related

Netbeans project to scripted build

I'm trying to convert a Netbeans 6.9.1 project into a scripted build (without netbeans). Of course, it fails (or I wouldn't be asking for help).
In the failure it says that the org.apache.commons.httpclient package does not exist. (Of course, it worked when we ran the build in Netbeans).
Now I know exactly where the commons-httpclient.jar file is located in my project structure, but I can't seem to tell it to the compiler via the ant build files and the netbeans property files.
Perhaps related to this is when I ran "ant -v" to build my software, it said,
Property lib.mystuff.classpath has not been set. This variable is important, I guess, because
the file nbproject/project.properties uses lib.mystuff.classpath in its definition of javac.classpath, which of course tells the Java compiler where to find the JARs.
So...when moving a Netbeans project to a netbeans-independent scripted build, how can the build script set these properties? Also, how can I ensure that the jar file gets included in the ant build?
I appreciate any help I can get, as I am a Java newbie.
UPDATE AFTER ACCEPTING ANSWER FROM vkraemer:
There are a few best practices for build scripts for production software:
Put everything needed for a build under a single directory tree. (Netbeans = fail)
Put everything in source code control. (I did that)
The first line of the build script should clear all environment variables.
The next section of the build script should explicitly set all environment variables to values which are known to work.
The next part of the build should be able to execute using command-line programs such as javac, ant, cc, etc, and must not depend on firing up an IDE such as Eclipse or Netbeans.
It is a shame that Netbeans makes this hard.
I did a quick look in a Java Application project and found the following...
javac.classpath = ${libs.MyStuff.classpath}
libs.MyStuff.classpath is defined in %HOME%/.netbeans/6.9.1/build.properties.
You may be able to get by doing the following...
ant -Dlibs.MyStuff.classpath=c:\a\b\c.jar
You would need to do more if you have multiple jar files in the MyStuff library that you created in NetBeans.

Scala: Creating a small executable Jar relying on external Scala libraries

I'm trying to package a small application (still learning Scala!) in a "clean way". The goal is to have an executable JAR file. I've done the following:
packaged a JAR using sbt -> will work with
scala -cp myjarfile.jar MyClass
or
java -classpath path\to\scala-library.jar;myjarfile.jar MyClass
but won't work with
java -jar myjarfile.jar
because then scala/ScalaObject cannot be found. And no use adding a classpath on this last one, since the -jar option will ignore the -classpath option. Note that I have added the scala libs in my system CLASSPATH variable, but it seems to be ignored too when -jar is used.
added the scala library contents (by unzipping them first) to the jar created by sbt. This then works (the jar can be double-clicked and launched), but the manipulation is somewhat lengthy, and the resulting file is several megabytes big. Not ideal.
After hours of googling, I can see no way to create a small jar file that will launch when double-clicked and that doesn't involve a painful manipulation. What am I missing? I'm guessing there should be some way to define where the scala libraries are at system level. How do you deal with small applications that you want to be compiled and ready-to-run for efficiency?
Note that while I'm using sbt, I don't have a jar tool at hand (I'm relying on a JRE, not a JDK).
Thanks!
Pierric.
The following setup works for me:
have scala-library.jar in the same folder as the executable jar (and call java from there)
put this into your manifest:
Class-Path: scala-library.jar
Another option is to merge the contents of scala-library.jar into your application jar. The drawback is that this will increase its size. But you can use Proguard to strip unused classes from your final jar. I think there is an easy way of using sbt to package an executable jar using proguard.
Regarding the shrinking using Proguard, you can have a look at this page. It's about Android development; just ignore this part and have a look at the tables of the shrinking results. Some example applications shrink to less than 100kB.
Edit
Maybe you need to refine your question a bit. What are you trying to achieve? Do you want to install the program only on your system or do you want to distribute it?
If all you want is quickly launching a Java application without much impact of the JVM start-up time you can have a look at nailgun.

How do I get my java program written with NetBeans to compile with javac?

I have two java files. They run fine in NetBeans, but I have to compile them in javac, and run them on a unix machine since I'm connecting to a database on my school's server.
I've been searching online but everything is too specific, and I'm not too familiar with NetBeans.
What I'm doing is copying those two java files, and a .form file to a directory on my school's server, and then try to compile the two java files using javac. However, I'm assuming that it doesn't compile because it's missing all the information from the .form file?
I'm getting 100 errors when compiling the one of the java files, and they look something like this:
CARTSJFrame.java:380: package org.jdesktop.layout does not exist
.add(jPanel9, org.jdesktop.layout.GroupLayout.PREFERRED_ SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLay
My guess was that it has something to do with the .form file that I can see in my NetBeans project directory.
I've looked at previous questions about this but still haven't been able to figure this out. Any help is appreciated.
You are running into compilation errors because NetBeans includes a jar in your project's classpath automagically when you compile and run the project.
NetBeans uses the dot-form file to help it generate code for the layout. It is not used at compilation time.
There are a couple strategies that you can follow to resolve this problem:
Get the jar that has the classes...
http://www.findjar.com/jar/net.java.dev.swing-layout/jars/swing-layout-1.0.1.jar.html;jsessionid=252692AC0FBE9421C9436A748744ACED... and include that jar in your
classpath at compile and runtime.
Convert the code in your project to
use the javax.swing.GroupLayout.
This is a 'standard part' of Java SE 6. This SO answer covers how to convert between
org.jdesktop.layout.GroupLayout and
javax.swing.GroupLayout in
NetBeans.
I think your problem is that javac doesn't know where to find that library.
A typical Java project uses many libraries. Netbeans uses a folder "lib" to store those libraries and also some configuration files to automatically set the classpath. The classpath is a environment variable that Javac uses to 'know' where are the libraries.
When you use javac to compile the java files you need to provide the CLASSPATH variable first. Write all you dependencies.
An example:
Project/compile_all.sh
export CLASSPATH=$CLASSPATH:"lib/jopt-simple-3.2.jar":"lib/commons-io-2.0.jar"
javac src/*.java
Now you just need to run
sh compile_all.sh
And it compiles all you .java files

Packaging and Deploying Scala Applications

What is the simplest way to package a Scala application for use on a desktop PC? I'm guessing that would be in the form of a jar file.
At the moment I'm using SBT to compile and run programs
I'd be interested in solutions for machines that have Scala installed (and the library in their classpath), as well as those that only have Java.
The simplest (and the most consistent) way to package a Scala application, is packaging into a JAR (like you do with normal Java applications). As long as JAR consists of standard compiled Java classes, you may run "Scala" JAR, even if you don't have Scala runtime at the box (all you need is a Java Runtime and scala-lang.jar on the classpath). You may specify the main class (gateway to functionalities of your application) in the manifast
Main-Class: mypackage.MyClass
so that you'll be able to call it only passing a JAR name to java.exe.
java -jar myjar.jar
You may specify the main class in SBT project definition.
http://www.scala-sbt.org/sbt-native-packager/index.html
The plugin allows you to package in a variety of formats(zip, tar, deb, dmg, msi, etc) and in different archtypes.

How does Eclipse compile classes with only a JRE?

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.