I use cmd to compile my java applications, but when I use package to pack all the files, if I try to compile them in the working directory, even if I use full path of the file, that won't work. But when I go to the parent directory, the same command works. And only works in parent directory, children directories or other directories also won't work. Can somebody tell me why? or is there any solution that make javac work in the working directory, because I use Sublime Text, its builder configs and binds the working directory.
Read https://docs.oracle.com/javase/tutorial/java/package/managingfiles.html to understand how Java source files should be organized. Espcially,
... put the source file in a directory whose name reflects the name of the
package to which the type belong...
By default the source code for my.pkg.MyClass must reside in current_directory/my/pkg/MyClass.java. You can use other than current directory with javac option -sourcepath.
For example you have two classes in two different packages packagea and packageb laying in the parent directory.
And class names are ClassA.java and ClassB.java respectively, and you have packaged your classes in these packages then you have
to use javac command in parent directory like this:
javac packagea\ClassA.java packageb\ClassB.java
Your directory structure will be like this:
Parent_directory
|
|--packagea->ClassA.java
|--packageb->ClassB.java
Otherwise, you can use any build tool like ant or maven to compile
your code. You have to just run build.xml file and your all files will
be compiled.
Related
I am a Java newbie, just started learning it in college, and my class is using NetBeans but I'd like to use VSCode.
The professor told us that every Java file should start with:
package nameofthepackage
So that Java knows to which package the class (file) I created belongs to.
So I always create this structure:
I create a folder with the name of the main class, and inside this folder I make a src folder that will store the Java files. Eg:
MyJavaProject/src/MyJavaProject.java
I always name the main .Java file the same as the project folder name.
And when I compile I use javac with the -cp parameter to specify that the src folder is the classpath folder, where it should look for the .Java files I create.
I also always tell javac to compile all the files inside the src folder, using the * wildcard.
The issue is that with this line on top of my .Java files, javac compiles all the files, but I can't execute the bytecode because it complains it can't find the classes I created, even the main one.
As Soon as I remove the package line from the top of the files, I can compile and run the code.
So far it's good, but for any more complex projects this is gonna be really annoying.
Any ideas how can I fix this?
You need to specify the full name (with package) of the class that contains the main function.
Assuming the class MyJavaProject is the one containing your main, that would be:
java nameofthepackage.MyJavaProject
This is assuming you built it with
javac -d . MyJavaProject.java
It would create your target class in a directory structure like:
nameofthepackage\MyJavaProject.class
I have created a java source file in Source folder and also created a folder named classes which is supposed to contain my class files .
javac -d classes Source/TestMyAnnotation.java
puts my files in the directory structure classes/Source/...
Now I want to run that compiled classes but
java -cp classes/Sources TestMyAnnotation
throwing NoClassDefFoundError
Where am I doing wrong? How to do it in a correct way?
Sources is not a package folder; it's just the directory that holds your java source, so you don't need to put it on your classpath. If you take a look inside your classes directory after you have compiled your java source, you will see a file TestMyAnnotation.class - there's no Sources directory underneath it, so you don't specify it on the classpath.
You will be able to run your class like this:
java -cp classes TestMyAnnotation
You can find more information on how all this works on Oracle's Managing Source and Class Files Page
I am a newbie in java I Want to know that what is the default directory for packages in java ? I Mean if i compile a java file which contains a package statement,and i compile it without using -d option in javac command,then where will be the package created ? eg.
package test;
class Test{}
and compile it using javac Test.java
then where will be the package created?
Thanks
If you don't specify -d, the class file will be created in the same directory as the source file.
That's fine if you're already storing your source in a directory structure matching your package structure (and if you're happy for your source and class files to live in the same place) but if your source structure doesn't match your package structure, you'll basically end up with class files in locations where they can't sensibly be used.
Personally for anything other than quick throwaway (usually Stack Overflow :) code I would make the following suggestions:
Avoid using the default package
Keep your source code in a directory structure (e.g. with a root of src) matching package structure
Generate class files into a separate directory structure (e.g. with a root of bin or out or classes)
(Sorry, misread the question to start with.)
-d option in javac command is use to specify where to generate the class file,if you don't specify it,then the .class file will be created in the same directory where your .java file is present.
There is no directory created when you do not specify the package!
all the .class files will be created directly in the output folder
public class A{}
if you compile this to output folder ,
output/a.class is created
Say I have a class TreansferBook that is part of org.cadenhead.library package
I read that Java 2 SDK and Java Tools looks for packages in:
1 The org\cadenhead\library subfolder of where the java command was entered
First question is, in Eclipse, where is the option to modify where the java command was entered?
I don't even know how u would do it on the command line; would it be like
C:> cd
Would directory be the workspace and project directory? And are packages in side this direcytory
C:\\ java org\cadenhead\library
2 The org\cadenhead\library subfolder of any folder in your ClassPath setting
I know the classpath tells the jvm where classes and packages are located. Again, as I asked above, is this a subfolder of workspace/project/?
3 The org\cadenhead\library subfolder of a java archive fil (JAR) in your Classpath
Finally, what is this?
When using an IDE like eclipse, you don't set anything. The IDE already knows where your code is and can set the classpath appropriately. This is not something you need to worry about, it's one of the perks of using an IDE.
Where java is run from is not important. What is important is that the classpath for the java command is set to contain the root directory of your class files (the parent of /org in your case) and any dependent jar file you may have.
Some things to about class loading and Eclipse:
The JVM (and the compiler) look for classes in every directory and in every JAR file listed on the CLASSPATH. For example, if you have a class path like: CLASSPATH=c:\workspace\myproject;some_library.jar, then the VM will look for a class org.cadenhead.library.SomeClass in
c:\workspace\myproject\org\cadenhead\library
In the directory org\cadenhead\library contained in the JAR file.
In Eclipse, the default directory on the classpath is the "bin" directory inside your workspace, plus all linked projects and JAR files.
The location from which the "java" command was called is the project directory itself.
How does one create a package for a Java Project through the Unix command-line?
I am looking to find a way to create a package for my Java files similar to the way Eclipse does, but in the command-line.
Packaging is internal to your code; you don't need any external tools. All you need to do is insert a package your.package.name; declaration at the top of each .java file, and stick them all in a /your/package/name directory (generally under the src dir of your project), and they'll be packaged.
The package structure is merely a directory structure. So I would use
mkdir -p com/oopsconsultancy/example
vi com/oopsconsultancy/example/Example.java
The Example.java would have to have the appropriate package statement inserted automatically (I had a vim plugin for this ages ago)