first off let me start by saying I am completely new to Java, but to give you an idea of how new; I started reading lots of books, examples and so forth and began programming Java using Eclipse about 2 months ago. However, I found a really cool bit of advise about using notepad and the terminal to program instead. Kinda crazy for a newbie to go the hard route, but I love a challenge and I'm serious about learning.
So, In Eclipse I had a really good grasp of how to import, add jars compile etc. When I started using pico and using the terminal (I'm running ubuntu) to compile all went really well, until I wanted to use packages. I've spent two days pulling my hair out because no matter what I do I can't figure it out.
I'm trying to use the acm.jar (which I have many times in Eclipse) however I'm completely lost on how to use it when compiling from the javac in terminal.
So what I'm asking for, is for someone to explain the process getting my jar file to work.
All I'm using to create my java programs is the pico (or notepad) and the javac in the terminal.
To compile and run a java class using external libraries, you have to add that library to the classpath. The classpath is the set of places where the java compiler and the JVM look to find any external libraries/classes that it needs during the process of compiling/executing.
Setting the classpath can be done in 2 ways:
Set an environment variable called CLASSPATH
Set it when your run javac/java
Setting the classpath when running javac/java is done like this:
javac -cp path/to/jar1:path/to/jar2:path/to/jar3:path/to/dirContainingClasses
yourMainClass.java
To run:
java -cp path/to/jar1:path/to/jar2:path/to/jar3:path/to/dirContainingClasses
yourMainClass
: is used as a separator on Linux, for windows use ;
Assuming your source files are in src. Assuming you want your compiled classes to be in classes. Assuming your source files reference classes that are in lib/acm.jar:
javac -cp classes:lib/acm.jar -d classes src/com/foo/bar/MyClass.java
will compile the class com.foo.bar.MyClass and put the generated MyClass.class file in classes/com/foo/bar.
You need the acm.jar file in the classpath. That's what the -cp lib/acm.jar option does. You also need classes in the classpath, because MyClass probably references other classes that you have already compiled and that are in your classes directory.
To run your class, it has to be in the classpath, and acm.jar as well:
java -cp classes:lib/acm.jar com.foo.bar.MyClass
As you see, the classpath contains jar files, and directories containing the folder hierarchy which matches the package hierarchy.
I wouldn't use javac from the command line directly, though. Try using a real build tool, that will build all your classes at once, like Gradle or Ant. Maven is also very popular, but I hate it with passion.
Related
Dear Java Programmers,
JavaBib (http://code.google.com/p/javabib/) is a simple open source java package for which I would like to generate bytecode.
The source can also be obtained by version control via
svn checkout http://javabib.googlecode.com/svn/trunk/ javabib-read-only
Although it consists of only a few java files, I am not able to compile it using
javac *.java
I get
cannot find symbol
error messages for nearly all of the files.
The source files of the dom package all reside in one folder. Therefore, I think it should not be a problem with the classpath. I have tried different values for it nonetheless.
There are additional source files in other directories. I tried copying them all in one directory and run javac in this directory, but javac was complaining about the same imports.
Unfortunately, I have hardly any experience with java and can't fix the problem myself.
Therefore, I would be grateful for hints towards additional diagnostics in order to pin down the problem.
I use Ubuntu 12.04 as operating system and haven't set any classpath or sourcepath variable. At least javac seems to find imports like "java.util.list".
Thanks in advance for your help.
I finished writing a java program and am ready to export it. I made a runnable jar from Eclipse. Running the jar works just fine on my computer, but throws the "Could not find main class" error on any other computer (including my other computer that I write Java on).
Whenever I search around for other people having the problem, it's always the same answer: Check the manifest file. I'm not seeing any problem with mine (Plus, can't imagine why it'd work on my computer but not someone else's)
Manifest:
Manifest-Version: 1.0
Main-Class: my.quick.monster.QuickMonster
And I've also tried:
Manifest-Version: 1.0
Class-Path: .
Main-Class: my.quick.monster.QuickMonster
Both work for me, but not other computers.
Thinking about the things that might be going wrong, here are a few other things to check:
make sure that there are no spurious characters (tabs, spaces) at the end of the lines.
check that the main class is actually in the JAR file with the right name.
on the machine that works, try changing your current directory and seeing if it still works.
check that you are using the same version of Java on each machine. Run java -version to check.
make sure that you are running it as an executable JAR; i.e. as java -jar foo.jar not as java -cp foo.jar.
(One theory is that the JAR is working on the one machine in spite of the manifest; e.g. that it is finding the class via the classpath in your CLASSPATH environment variable or something.)
To summarize for other folks, the OP's problem turned out to be that he had compiled his code with / for Java 7, and was trying to run the JAR on older Java installations. That wasn't working because of the classfile version numbers.
You can compile your code so that it will run on an older version of Java, but you need to use the -target option when compiling, and you ought to use the -bootclasspath option to compile against an rt.jar from the oldest Java version. A typical IDE will simplify this by allowing you to specify the target build platform, but it is worthwhile understanding the technical details, for cases where you are not using an IDE.
(I'm surprised that the java command didn't mention the classfile version number in the error message ...)
Make sure the MANIFEST.MF file contains a blank line at the end. If the Main-Class definition is on the very last line of the file, some class loaders ignore it.
Do not ever use 'eclipse-jar-worked-fine-on-my-computer'. I use maven shade jar plugin which excellently build a ready to run jar with all the dependencies, specified main class, etc.
EDIT:
What is the wrong with eclipse-builded-jar is that you won't been able to build it w/o elcipse. Maven is the common tool widely used to build packages of any kind. It's automated, and means that it can be used in CI environmet, etc. And the goal of a good developer is to write code so that it can be easily moved to CI.
However, if it's not a regular task, assuming to make just once/twice, theen, maybe, 'eclipse' solutio has also some benefits. But, I answered keeping in mind some cases of my past when people build packages in GUI just because they didn't manage to do it in maven.
So, I hope there is enough arguments for maven vs eclipse, so please stop downvote :D
This question already has answers here:
What is a classpath and how do I set it?
(10 answers)
Closed last year.
I am trying to compile a library I wrote, using javac and I am getting the error: package org.json does not exist. My program includes org.json.JSONArray and org.json.JSONException.
I have this package installed on my computer because I have successfully compiled android apps that import org.json libraries. I'm pretty sure all I have to do is specify a -classpath but I have been unable to find where these files live on my system (ubuntu 10.10 64-bit sun-java6).
Having been unable to find these on my own system I downloaded the org.json files from here, but I was unable to compile them individually because they were co-dependent on each other.
So I have a couple questions:
Does anyone know where the org.json
package lives from android sdk
install?
Where might I find a tutorial
explaining these basic concepts
regarding compiling, and javac.
Whatever external jars you need to compile with should be on the classpath when you compile. The most non-invasive way to do this is do add these items to the javac command line such as
javac -classpath /path/to/json.jar;. -g YourClass.java
or more likely if you use an IDE, add these jars to your referenced jars of the project in your IDE.
It usually isn't a good idea to pollute the global $CLASSPATH variable, as this then gets pulled in for everything you do with java, which may cause unintended conflicts.
Wherever you like. What you need to do is examine your CLASSPATH variable, and make sure it includes the directory with your library.
Here's the first thing:
$ echo $CLASSPATH
and you'll see your classpath as it is.
Now you need to find the jar file containing the org.json; consult the documentation, but it may be something as simple as json.jar. On most LINUX systems you can then just run
$ locate json.jar
And you'll get a path name for the jarfile. Make sure that path is part of your CLASSPATH and you'll be in fat city.
Oh, and the "Getting started" tutorials at Sun Oracle are the easiest place to start.
Actually, having looked at the files, they may not be packaged as a jar file. In that case, you want to put them into your sources starting at some top directory (src in this example.)
/src
/org/json/ ... put the json files here
... put your files here
and when you compile, they'll all be included, which will resolve all the dependencies.
Again, the place to look for first steps is that tutorial.
use "java" command instead of "javac"
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.
I am new to Java (and Eclipse) but I have used .NET (and Visual Studio) a fair amount. I also know about compiling C/C++ code and things like that. I know that at the end I get either an EXE or a nice binary file that can be run from the command line.
I have been making a Java utility that uses some external libraries. I need to compile this into an executable that I can run from the command line on a unix machine, but I cannot find any way to do this.
I can build and run/debug in Eclipse, but that is no use to me as this code will be run on a webserver. I just need all the dependancies compiled in to one file but after hours of searching on Google, the best thing I could find was the Fat-JAR plugin for Eclipse and after using that I just get the following error when I try to run the file:
Exception in thread "main" java.lang.NoClassDefFoundError: Network/jar
This is really confusing me and as it is such an essential thing to be able to do I am sure I must be missing something blindingly obvious, but as I said, after hours of searching I have gotten nowhere.
I really appreciate any help you can give me. Thanks.
If you build your java app using Maven (which can be done with every major IDE), then you can use the maven Shade Plugin to build a shaded jar file; this is a jar file with all of its dependencies included.
A shaded jar can be run from the command line like this:
java -jar myjar.jar command line options
You're doing something standard and you're using eclipse. This means, in your case, Maven is your friend. Download and install the M2Eclipse plug-in. Maven is best at managing dependencies. So, creating a jar with dependencies included will be very, very straight forward. There are thousands of examples on the web and in StackOverflow. If you have problems setting it up, comment on this and I can point you in the right direction.
Sounds like your class path on the server needs to be modified to pick up the jar file containing the Network class. How are you executing your program? What path(s) are you putting in the -cp option?
If you are not sure how to find out the contents inside a jar file, run jar tf , this will list the packaged classes. Validate that one of the jars in your CLASSPATH has that class it says missing.
Give us more details and we can help solve it.
I think I should first explain some basics. A Java class can be run as an application if it has a public static void main(String[] args) method. If it has this method, you can run it from command line as:
java my.package.MyClass <attributes>
Before launching your app, you need to make sure that all required .jar files (and the root of your own class folders, if you did not make a jar from your classes) are in the CLASSPATH environment variable.
These are the absolute basics. When you are building a more complex app, or an app for distribution, you'll probably use maven or ant or some other tool that makes your life easier.