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.
Related
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
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.
I'm used to writing java in eclipse and using it to import JARs, set up workspace, etc. however, when I want to deploy a project to my server and call it from a bash script like java Main arg1 arg2 something isn't write I get:
java.lang.ClassNotFoundException
is my classpath set wrong? what is eclipse doing behind the scenes? when I do
echo $CLASSPATH
I always get a blank line. The below (executed from bin folder) doesn't work either:
java -cp "~/Code/Java/SQL/MySQLAccess/bin;/usr/bin/java;/Users/Me/Code/Java/SQL/MySQLTest/mysql-connector-java-5.1.18-bin.jar" Main arg1 arg2 ... arg7
this gives me a java.lang.ClassNotFoundException: Main error
your class path is not set correct, you can use command java -classpath path_for_java javafile argumnets
As far as eclipse is concerned you can go to project->properties->java build path->libraries and you will get to know from where eclipse is referring to java libraries.
If you plan to be moving this around and deploying to potentially different servers, look into exporting your project as a jar. You can include the sql jars inside your jar and your invocation from the script will look like
java -jar Main.jar
From Eclipse, you can go to File->Export, then pick 'jar' from the menu and follow the steps.
Your manifest will be something like
Main-Class: Main
Class-Path: mysql-connector-java-5.1.18-bin.jar
I want to create our own jar which has some simple methods like a print method.
I created the jar of that java project and imported it into an other project I am building path also.
The problem is that I am able to create the object of the classes which reside in the jar but I am unable to call the methods of that class.
i am using eclipse 3.4 (Eclipse Ganymede (3.4))version
Sounds like if you are successfully building the JAR that you are not including it in the classpath when you compile / run your application. You can do that if you are compiling/running from the command line with the -cp or -classpath option. Both perform the same function.
To compile:
javac -cp .:PathToMyJar/MyJar.jar MyMainClass.java
To Run:
java -cp .:PathToMyJar/MyJar.jar MyMainClass
The above commands will look in the current directory ('.') and in the MyJar.jar file specified. On Windows you will separate the items in the classpath with a semicolon and on Linux/Unix/OS X you will use a colon.
If you are using an IDE you will need to somehow add the JAR file to the classpath for your project. How to do that is IDE specific.
I would like to use an external library (e.g. Google's Guava) for my Java program. I use Eclipse, so I downloaded Guava's jar (and source) and followed Adding a Java library to the project classpath to add it to Eclipse and to the buildpath of my project.
This works fine: I can run the program from Eclipse and from the runnable jar I export from Eclipse, but I get an error when I try to run directly from the bin/ dir, as I used to do before:
Exception in thread "main"
java.lang.NoClassDefFoundError: com/google/common/base/Joiner
What should I do?
If you're running the class file directly from the project bin directory then you may have to specify the classpath manually:
C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses MyClassHere
Youll have to tell Java where to find the library:
java -cp <path-to-lib-jar>;myJar.jar my.package.MyMainClass
or if you wanna use a jar file you can set the library path in the MANIFEST
check here for an explanation.
Have you tried java -cp guava.jar ... ?
To run the program on console as precise as possible with when you run it from Eclipse, you need to run it from the root directory of the project (not from bin) and don't forget to mention the classpath (http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/tooldocs/windows/classpath.html)
So for example on root you will run:
java -classpath lib/guava.jar;bin packageName.className