Compiles with NetBeans but can't compile or run from bash - java

I have been researching this for weeks and can't seem to get it figured out.
I have a Java program that I have written using NetBeans. It has several imports or .jar files it relies on. It runs fine in NetBeans. But I can't figure out how to call the .jar files and compile from the bash command line. I am using a Mac. I have read several posts on this and none so far have made sense to me. There are 26 imports being used in the program. I don't know if I need to use Ant or specify -CP or Classpath to compile. Surely I don't have to type each one of the .jar files out to compile this from the bash command line?
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)

You should be able to simply use ant with the build.xml in the Netbeans project directory. It should "just work" for you. The project will likely not build WITHOUT Netbeans installed (if you tried moving the project to a different machine, for example), but with Netbeans, it should work out of the box.
If you don't have Ant installed, you'll need to install it.
Addenda:
To build it, if you have Ant installed, you should be able to simply go the project directory, where the build.xml file is, and type "ant", and it should build and put stuff in the dist directory.
If you go in to the dist directory and type java -jar yourapp.jar, it should run, because the manifest in the jar will point to the nearby lib jar files. If you want to distribute the app, there are different options for java, such as making a Mac compatible application, or a Windows EXE, you'll need to search for those. Or you can simply distribute the contents of the dist directory and write a script to do that whole java -jar yourapp.jar command.

If you are using Java 1.6 or above, you can toss all 26 jar files into a folder and simply add
-cp folder_name/*
as your classpath argument.
Some other options are 1.) type all 26 jar files on the command line (using the -cp argument as you have mentioned) 2.) use Ant or Maven or some other build tool and list those 26 jar files in the config file for said build tool or 3.) write a quick-and-dirty shell script that will set the CLASSPATH environment variable for you and then run your javac command.

Related

External jars not loading in built project. NoClassDefFoundError

So, I have exported my project in both Netbeans and Eclipse and when I try to
java -jar myproject.jar
I get this prompt
In my project I have some libraries which are located inside of src in Netbeans and out of src in Eclipse as it should (please correct me if I'm wrong) The libraries are included via:
Java Build Path > Add JARs...
I've done some research and it seems that I have to change my JAVA CLASSPATH or somethng like that but I don't know exactly how to do it.
The project works perfectly when I compile it and run it, but it crashes after I build it into a Jar file.
By the way, if it isn't clear enough I'm on Ubuntu 14.04
You need to create the path for the jar files and pass it on the command line.
Something like this:
ftp_jar=${Utils_home}/bin/ftpClientUtil.jar
net_jar=${Utils_home}/bin/commons-net-3.3.jar
jsch_jar=${Utils_home}/bin/jsch-0.1.51.jar
java -cp .:$jsch_jar:$net_jar:$ftp_jar com.myplace.ftputils.SFTPClientUtil $*
Run your program as:
java -cp .:[path-of-lib1.jar]:[path-of-lib2.jar] -jar myproject.jar
replace [path-of-libX.jar] with actual path of your libraries.

Use JAR Files In Bash

I've been looking for an answer, but still a little confused...
In eclipse I just configure the build path of the project and add the jar files that I need, and everything works just fine. When I try to run my programs via Bash, it's not finding my imports.
How do I run my Java programs in bash or on another server with the required jar files?
Thanks
You specify the CLASSPATH. Either through the environment like
export CLASSPATH="a.jar:b.jar"
java com.stackoverflow.Main
or explicitly via the -cp command line option like,
java -cp a.jar:b.jar com.stackoverflow.Main

Java - how to run console program in windows

Hi I wrote wordquiz program on Java. Using Eclipse in Unix.
In my linux machine it works fine.
Here is a source code https://github.com/HighlanderGe/Words
So, only basic package is used.
In windows compilation of such code as jar doesn't run. Neither in Mac.
As I guess problem is that in linux it is made to run from console, and console is comething very native for linux, but in Windows and I think in Mac too, cmd should be called, and from there it somehow to run.. but I bet cmd has no idea what is java. So some java console is needed for it?
The problem is not with the Mac or Windows, the problem is that you did not setup your workspace in Eclipse the same way on your different computers.
You can build your program on the command line in all environments in the same way. You just have to know the right steps.
First of all, there's an error in your code on line 25 in WordDatabase. Instead of:
dictionary = new ArrayList<>();
it should be:
dictionary = new ArrayList<String>();
After that, you can build your code like this:
javac -d . *.java
And run it like this:
java wordquizz/Wordquizz
This should work in any system that has Java, you just need to figure out how to setup your workspace in Eclipse the same way on your different computers.
UPDATE
I forked and converted your project to a Maven project:
https://github.com/janosgyerik/StackOverflow-Words
After you clone this to your PC, you can import into Eclipse using the File | Import... menu, and then Existing Maven projects option. It should work on all operating systems.
Maven is a recommended tool for building Java projects and it's a good thing to learn. After you install maven, you can build the project with:
mvn compile
You can package the project into a jar file with:
mvn package
You can run your code with either of these commands:
# needs 'mvn compile' first to generate classes
java -cp target/classes/ wordquizz.Wordquizz
# needs 'mvn package' first to generate the jar
java -cp target/wordquizz-1.0-SNAPSHOT.jar wordquizz.Wordquizz
If you like these improvements, merge from my repo soon. I won't keep it forever, I will delete it at some point.
UPDATE 2
To make a jar executable, you need to add inside a manifest file like this:
Main-Class: wordquizz.Wordquizz
You create the jar file with a command like this:
jar cvfm package.jar manifest.txt wordquizz/*.class
I updated my GitHub repository, so that now if you run mvn package, it will automatically add the right manifest, and the generated jar file will be executable.
If you installed java under windows you have to adjust the Path-variable, so the cmd knows where the java executable is. A good tutorial on how to set this up you can find here:
http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
After that you can simple go to the directory your source code is in and use the same commands as under linux to compile and run your application.

Compile and export in java via Terminal

I have a Java program in Eclipse on Mac currently, and I normally have to use multiple clicks just to export my code into a .jar file to test on my server. I would like to automate the process via terminal.
Basically, I compile my code usually by selecting the project
Export as Runnable JAR file
Select library handling: extract required libarires into generated JAR
Select export destination and hit done.
How can I do this via terminal? I assume this would first require me to compile the Java file, then to convert it to jar is a whole another step.
Help would be much appreciated.
You can create shell script that does it. This technique is obsolete since ~1998. So, use one of popular build tools. If you are starting now take a look on Gradle. Although there are a lot of other tools: good old ant, maven, buildr, ivy etc.
You can script all these activities using a build script. Several libraries exist for this, but Apache Ant is a good place to start. Ant build scripts can be run from command line or within eclipse, and will do all compilation, packaging and (some) deployment for you with a single command.
http://ant.apache.org/
Create default entry point manifest file as in : http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
Open Terminal and create an executable jar file like this:
Sample Script:
#!/bin/bash
# set CLASSPATH if needed
cd workspace/src
javac -d . *.java
jar tf exported.jar .

Running Jar on Snow Leopard with classpath

I'm running snow leopard and I just wrote a Java class on eclipse. The eclipse project references a user library which itself points to a bunch of jar files I've got somewhere in the system. When I run the app through eclipse, everything goes smoothly.
Then I export the class as a jar file and try to run it form the terminal by typing:
java - jar myApp.jar
It throws a java.lang.NoClassDefFoundError exception, meaning that it can't find the libraries I try to reference.
Knowing that my user library jar files are in /Users/myname/tempJars, I also tried to either mention the classpath using the -cp option
(java -cp /Users/myname/tempJars -jar myApp.jar) or to directly reference it in the manifest file. Both attempts failed and the error is the same.
These libraries are Java 1.5 libraries, so I thought I should try and reference another java version by mean of the JAVA_HOME environment variable. I built the following script:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Home
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=$CLASSPATH:/Users/myname/tempJars
java -jar myApp.jar
Again, no good. I googled how to execute jars in snow leopard, found the 32/64 bit big deal in some places and even tried executing with the -d32 option, but still to no avail.
Since the same code executes just fine in eclipse, I'm pretty convinced it's just a matter of setting up the JVM so that it includes the user libraries correctly.
Could anyone help me with this?
Thanks in advance.
You need to list the JARs themselves out on the classpath, not just the directory containing them. (Listing the directory is for when you have unarchived .class files in the package hierarchy lying around)
You will probably need to do -cp /Users/myname/tempJars/libA.jar:/.../libB.jar
I believe some (but potentially not all) JVMs support wildcards so -cp /Users/myname/tempJars/* or some variant thereof may work.

Categories

Resources