Though it's probably reccomended one uses and IDE for coding advanced java projects, I personally prefer running almost entirely command-line (using gedit as a text-editor). So please don't just tell me "Just use eclipse!" or something :P
My question is what the method of creating a package in java is by a command.
I'm not talking about packaging an application that runs in the command line, I'm talking about making a package in the command line. Am I making a text file? Am I making a directory?
Relatedly, how does one link to related libs and natives without use of an IDE?
I know I'm being really awkward here, but I really prefer the control one gets working in command line.
There are three parts to it: (1) create directory structure; (2) indicate package in java file; (3) compile it.
For example, if you want to create package com.mycompany.myproject, then you need to start in the base directory for your project and then:
(1) create directory com/mycompany/myproject
(2) create java files in that directory, stating package com.mycompany.myproject in them;
(3) compile the files, for example, with javac -cp . com/mycompany/myproject/*.java
You may want to specify a different output directory so as to not mix sources and compiled classes.
If you need to use external libraries (.jar files) to compile, then you need to use -cp or -classpath command-line parameter to javac tool to specify them, e.g.
javac -cp .:some_library.jar:lib/another_library.java com/mycompany/myproject/*.java
It may be a good idea to put all external libraries in one place, e.g. lib subdirectory of your main project directory. And, by the way, the above javac command assumes unix-like environment. If you're on Windows, then you'll need to use ; for path separation.
packages are just directories on the filesystem.
so your package: com.mycompany.util corresponds to a directory com/mycompany/util.
When running and compiling etc your current workding directory should be where that top directory is located.
To include libraries, include them in your classpath when compiling and running. For example make a Project directory myproject and under that have your java-files and packages under myproject/src/ and libraries that you use under myproject/libs/
Then when your current workding directory is myproject execute java -cp .:libs/*.jar or the same with javac.
But I suggest you look into using ant or maven.
You can get along just fine on the command line by using a packaging tool such as Ant or Maven. Maven is especially handy because it is a higher level tool that already knows how to build various project types: command-line apps, webapps, libraries, etc. It also handles library dependencies by downloading them from repositories.
Java Package is just a directory structure, so a simple way of creating a Package lets say com.organization.test in terminal will be
mkdir -p com/organization/test
// to create a new directory and a subfolder that will be your package
$ mkdir -p parent/child
// to move into it :
$ cd parent/child
//to create an empty java file
$ touch MyClass.java
//to edit current file
$ nano MyClass.java
package child;
public class MyClass {
}
PS: The directory structure on your computer is related to the package name. That means when you edit .java file it needs to have a package declaration(your package) otherwise you will have a default package (ex: java.util.*).
Related
When compiling java code the I have been told the compiler must be run from the top of the package.
That is if I am trying to compile Test.java which is in tools.testing I have to first set the top of the package hierarchy, the folder containing /tools in order for it to work.
The class I am trying to compile uses another class contained in the same package and as such passing the full path of the code to the compiler prevents it from seeing the other class (as it doesn't search current directory and instead searches for the package inside of itself: ./tools/testing when it is already in /tools/testing )
I wanted to know if this was always the case or if there was a way to, for example: provide the path to the top of the package (since passing full path will not work for me) as an argument of the javac command or something similar ?
Thanks !
You should use an Integrated Development Environment (IDE) like IntelliJ, Eclipse or Netbeans. In an IDE you can create a Java project which has a directory acting as the 'source root'.
If you use Maven as your build tool the default location for such a directory is /src/main/java/ (this is the de-facto standard for Java projects at this time).
The IDE will automatically compile your Java files for you and allow you to run them easily during development.
If you want to run the application stand-alone you have to package it in some way. One simple and effective way is to generate a .jar file which contains all the .class files and other files you need (like images, .properties files etc). If you specify a pom.xml file for your project (that's Maven again) and set the packaging to jar Maven will automatically create a .jar file for you. You can even make the .jar file runnable with some additional settings.
See also this answer for some more info about packaging.
I have very simple case. In folder D://redbacktree I have two files .java.
I want to compile them into folder D://redbacktree/bin.
What I tried is :
javac -d bin *.java
This created folder redblacktree inside bin folder and put .class files into
D://redbacktree/bin/redblacktree/
But I don't want javac to create redblacktree folder inside bin folder.
How to compile and run this correctly?
I'll create an answer from my comment instead.
So first off; if you have package declarations (which is good practice to organize your code and modules) your files will end up in a folder structure matching your package structure.
I'd suggest reading the man pages for javac and javato see all the flags and options. I'd also suggest using some kind of build tool and follow the java "convention" of how to organize your source and class files. Read more here.
Maven, Gradle or Buildr are three examples of build tools for Java.
Too "fix" your current problem you can run your compiled java classes using:
java -cp bin redblacktree.<name of your class>
i.e you need to use the fully qualified name (package path + class name) when telling java which class to run. Also -cp specifies the (root-)classpath, where your compiled classes can be found.
Maybe your java files have a package declaration?
package redbacktree;
The compiler creates a separate folder for each package. You could try to use the default package (i.e. omit the package declration completely), although it is not a good practice to use the default package.
I have an assignment where I have to submit my code in flash drive.
I did all coding in eclipse and getting all output. I tried running it through command line and it works. I copied all java files and class files to flash drive and tried again through command line it works in my laptop. but when I take that dive and put it my desktop computer and run through command line, it can not recognise the classes that are in the package.
I have a package a1.cis568 under this package I have several classes. main class is A1 and other classes are Circle, Point, PlaneCircle, EHashtable, CHashtable in the same package.
I have to use following line on command line to compile and run my program through flash drive, (don't have to change any classpath while doing so)
E:>
E:>javac -d . A1.java
E:>java a1.cis568.A1
When I was searching for solution I found that I can install JDK on flash drive and run the code. I tried installing JDK but it doesn't solve my problem.
If you are using eclipse you can easily export the project as a runnable jar. Copy the jar file and source to your flash drive. Execute from command line via the following
java -jar [runnable jar filename]
You seem to have troubles compiling your source code!
Since you navigate trought the package hierarchy to where A1.java is located and then try to compile, the compiler cannot find other classes which A1 depends on and which are not in the same directory. You have to tell it where to find them, this done using javac's option -sourcepath.
So from the same location of the class A1.java you could compile your code using something like this:
E:> javac -d . A1.java -sourcepath ..\..\
Here ..\..\ is used to tell that the sources can be found two directories above the actual directroy (based on your package tree a1\cis568\).
But to make things easy I would suggest you compile from the root of you source code tree. In this case the sourcepath and classpath are the actual working directory per default.
So navigate in the commad prompt to the location where the package/directory a1 is, then compile like this:
E:> javac -d . a1\cis568\A1.java
The comipiler should be able to find all classes on which A1 depends on (assuming they all exist with the correct package inside the root of your source code).
Run with
E:> java a1.cis568.A1
I have a client/server program that attempts to send and receive an object.
There are three packages: server, client and shared
shared contains only the Message class
I put Message.java from shared package into the same folder as calcclient package source files and calcserver package source files.
I compile using the line: javac -classpath .; (long list of client or server.java files) Message.java
They can compile.
Then I change directory up one level and ran with: java -classpath .; .Main
When I use Netbeans to run, the entire program works as per normal. But not if I run from command line. If its executed through command line, the program will work until it needs to use the Message object. Then it will show a NoClassDefFoundError
Am I putting the right files at the right places? How do I get the program to find shared package through command line?
The files are not in the right place. The Message class belongs to a different package so it shouldn't be living with the other classes. From http://java.sun.com/j2se/1.5.0/docs/tooldocs/findingclasses.html :
User classes are classes which build
on the Java platform. To find user
classes, the launcher refers to the
user class path -- a list of
directories, JAR archives, and ZIP
archives which contain class files.
A class file has a subpath name that
reflects the class's fully-qualified
name. For example, if the class
com.mypackage.MyClass is stored under
/myclasses, then /myclasses must be in
the user class path and the full path
to the class file must be
/myclasses/com/mypackage/MyClass.class.
If the class is stored in an archive
named myclasses.jar, then
myclasses.jar must be in the user
class path, and the class file must be
stored in the archive as
com/mypackage/MyClass.class.
You have a couple of options:
The best solution is to take the time to learn Ant. Netbeans projects are built with Ant, which is a really great feature of Netbeans in my book, and you can open up the build.xml in your project and find a reasonably well commented description of what Netbeans does to build your project. And really I don't think there would be many places around that run builds from the command line so learning something like Ant would be a great help.
The next level down in sophistication would be to manually build a Jar for your shared package and put it somewhere on the classpath.
The most basic approach is just to compile the java files into class files and put them in the appropriate directory reflecting the package name as explained in the quote above.
If you build your project in NetBeans, you'll see that there is a dist folder where you can find your project in binary code. After building the source code, NetBeans specifies how should you start your project from command line.
If you use this and the problem persists, you should rebuild your Message class as a library, link it to the project using NetBeans and the project should work from command line using the command specified in NetBeans.
If you want to manually compile your source files, I think the best solution is to google something like this:
manually compile Java source code
Quote the classpath value if it contains spaces.
java -classpath C:\Program Files\java\...;C:\...
^
This is what's killing you slowly.
Try it this way:
java -classpath "C:\Program Files\java\...;C:\..."
I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth