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

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

Related

how to keep jar dependancy while checking in code

I have a scala project built with dependancy on a locally built jar file (java code). Once I need to check in my scala code into a different environment for building and deployment, what's the best way to keep my jar file in the dependancy?
I know that if I use the sbt dependancy from online modules, I don't need to worry, it will download the version and build, but what if I want to use my own jar file for this purpose?
This is in OSX, and code will be checked into linux machines, I am using intellij and sbt to manage my scala project. I also used intellij to build my external java lib into jar file and added dependancy of this specific path.
I hope there should be some generic solution, but I am new in JAVA and SBT
I got it figured out. Add the jar files under the lib directory right under the project will solve the problem. SBT will pick it up automatically and you can certainly check in the jar files like source code.

How to build a scala application that was created in eclipse scala plugin FROM THE CL

I have developed a scala application for the first time, but I have to deploy it with a "one-click" type script that can run and build the scala application from source WITHOUT ECLIPSE.
Since I'm completely new to scala I don't know how to tell it where all my source files are etc... to get it to build my app from the command line. I also have 2 3rd party .jar libraries that I need to tell the scala compiler to link to...
Any documentation on this? Or example command lines? My project hierarchy is:
src/packagename: contains all .scala
bin/packagename: contains all.class files
libs/ -> contains 2 .jar files I will need to import somehow
I'm working on debian linux
EDIT: I found this ability to export in eclipse so I created a .java file and called my main scala object from it. Then I exported as a runnable jar. However, when I go to run the new runnable jar "sudo java runnable.jar" it says "class not found exception: runnable.jar"
You should take a look at https://github.com/harrah/xsbt/wiki which is the common way to build a Scala project. Run through the tutorial in the wiki to learn how you should organise your directory structure, so that everything may run fine.
If you want to combine it with eclipse, checkout this plugin: https://github.com/typesafehub/sbteclipse

Eclipse: script compiler as part of a project

This question is not limited to lex and yacc, but how can I add a custom script compiler as part of a project? For example, I have the following files in the project:
grammar.y
grammar.l
test.script
The binary 'script_compiler' will be generated using grammar.y and grammar.l compiled by lex, yacc and g++. And then I want to use that generated script_compiler to compile test.script to generate CompiledScript.java. This file should be compiled along with the rest of the java files in the project. This setting is possible with XCode or make, but is it also possible with Eclipse alone? If not, how about together with Maven plugin?
(I might setup the script compiler as a separate project, but it would be nice if they can be put in the same project so that changes to the grammar files can be applied immediately)
Thanks in advance for your help!
You can add a custom "Builder" from the project properties dialog. This can be an ant script (with an optional target) or any other script or executable.
There are also maven plugins for ant and other scripting languages
If you just want to run an external program in Maven this is what you want: http://mojo.codehaus.org/exec-maven-plugin/ -- you can then run Maven targets from your IDE or command line and it should do the right thing either way.
To integrate with the normal compilation bind the plugin to the "generate-sources" phase and add the location where the Java files are generated to the "sourceRoot" option of the exec plugin. That way the compiler will pick them up.
Ideally you generate the code into a folder "target/generated-sources/MY_SCRIPT_NAME". That is the standard location for generated sources in the Maven world and e.g. IntelliJ IDEA will pick up source files inside of that location. Note that this doesn't work if the files are directly in "target/generated-sources".
The other option is to write your own Maven plugin, which is actually quite easy as well. See e.g. https://github.com/peterbecker/maven-code-generator

When I compile a file in IntelliJ on a maven project, what does it run?

I'm trying to make the move to VIM for my Java/Scala work. However it seems I cannot compile a single file using Maven. When developing I typically like to compile a file just to make sure it compiles before I compile the whole project.
In intellij I can choose to compile a single file that I'm on, what does that run behind the scenes so that my classpath is loaded with all my dependencies from maven?
The question boils down to what can I run from the command line to compile a file using Maven the fastest?
The fastest way to compile from IDEA is Build | Make. It will compile the changed files and all the dependencies. Compiling single file is also possible, IDEA will use Java Compiler API (like javac does) for such compilation.
IDEA doesn't compile using Maven in such cases.
I don't think single file is compiled using maven. I think IDEA just "knows" what what libraries are necessary (by parsing your pom.xml) and adds it to the classpath when running scalac.

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.