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.
Related
I came across this question:
What is a classpath and how do I set it?
and the first answer explainns what classpath is and how to set it:
.....First, let's suppose that MyClass is something you built as part of
your project, and it is in a directory in your project called output.
The .class file would be at
output/org/javaguy/coolframework/MyClass.class (along with every other
file in that package). In order to get to that file, your path would
simply need to contain the folder 'output', not the whole package
structure, since your import statement provides all that information
to the VM.
Now let's suppose that you bundle CoolFramework up into a .jar file,
and put that CoolFramework.jar into a lib directory in your project.
You would now need to put lib/CoolFramework.jar into your classpath.
The VM will look inside the jar file for the org/javaguy/coolframework
part, and find your class.
So, classpaths contain:
JAR files, and Paths to the top of package hierarchies....
but if java only looks for classes in directories specified by CLASSPATH variable how does java find classes from libraries that are part of JRE?
I have just shifted back from an IDE to Notepad to write a Java program. The program is using 20 JARs. I compiled successfully. When I decided to run the Java class file using
java -cp ".\\*" MyProgram
it was giving the standard error "Couldn't find or load main class....".
I was confused because when I used to run the java command with all files in an existing folder, it would just get those JARs as the current folder is already in the classpath. As the program is running from the current folder, I tried using -cp "." to include it explicitly in the classpath but that didn't work either.
Finally I was able to run the program with this command:
java -cp ".\\*;." MyProgram.java
I am asking this question to understand the actual logic behind Java's classpath.
Correct me if I am wrong, but I think that the JAR is just a standard archive in which all the packages are encapsulated in respective folders. If all the JARs are in my current folder including my main class file then why can't I run it with:
java -cp "." MyProgram
or simply:
java MyProgram
If the problem is with the multiple JAR files to include and that's why we used ".\\*" to include all the JARs in the classpath, then why do we have to explicitly include the current folder again in the classpath using:
java ".\\*;." MyProgram
To include all jar required to run your program in command prompt use wildcard *:
java -classpath E:\lib\* HelloWorld
You are using "." that defines current directory and "./*" defines all files in current directory.
The class path is a list of jar files and directories containing the classes and resources of your program. Mentioning a jar file adds its contents to the class path.
"./*" will get you only the jar files in the current directory, "." adds the current directory to the class path. This allows to access all classes (and the jar files as raw file resources but not its contents, i.e. the classes contained in them).
If you need both in the class path, you have to specify both.
You've answered your own question, sort of.
. means that it will look for .class files in the current directory.
JARs act just like a directory. So to have the abc.jar "directory" you would specify abc.jar in your classpath.
If you need both the .class files present in the current directory, and the .class files packaged into JARs found in the current directory, you would have the following classpath: -cp ".:*.jar
All the answers here are telling you to use the wildcard without extension (* or ./*) but this is a bad practice, you don't want Java to go look into irrelevant files, so specify the extension: *.jar.
"." means current directory not files in the directory
"./*" means all files in current directory.
So you want to use all jars in current directory so 2nd will work
I have a .jar and I would like to add my own .class to it and recompile. Is there anywhere I can do it?
Yes, and it's simple (if you already have the .class file - then there is no recompilation).
(Assuming you have the .class file already and just want to add it to a .jar. If you don't have the .class file, you need to write a .java source file and compile it to .class first using javac)
Jar files are actually zip files - you can use zip/unzip to create and unpack them.
unzip the jar file using a unzip program
add your class to the unzipped directory (*)
zip again (possibly to another name.jar)
(*) In step 2, you must put your .class file in the correct directory - correct meaning that the package name of your class must match the directory path where you .class file resides, relative to the .jar archive root.
For example if your My.class defines that it is in package com.nicky, then it must be found at com/nicky/My.class (where com is a directory in the archive root directory). If My.class has no package, they it must be in archive root directory.
No recompilation is needed - Java does linking dynamically at runtime - if the rest of the program needs to use class com.nicky.My, it will do so successfully if the class file is in the correct place in the .jar file.
You use the classpath parameter of the virtual machine (java -cp a.jar:b.jar:... ) to tell the java process where to look for classes - in case it would make more sense to package your class(es) in a separate .jar file..
--
Edit 1 / responding to your comment:
In case you a writing a new .java file, you need to first compile it into a .class using the javac command line compiler that comes with the jdk.
Every Java class belongs to a package, which is usually declared in the first line of the .java source file. Package names must match directory path location of the class (like in the example above), for both .java source files (for compilation to succeed), and for .class files (for dynamic loading / running to succeed).
You could also download and use a Java IDE (eg. http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/mars2 )to create your .class file. Create a new Java Project in Eclipse, write your class source code, click build (if it already doesn't build automatically) and then use a file explorer to take your .class file from the 'target' directory of the project on the disk.
If your new .java class depends on classes from the .jar you are trying to add it to, use
javac -cp your.jar com/nicky/My.java
to tell the compiler where the additional compile-time dependencies are.
If you are compiling from Eclipse, you need to configure that your project depends on your.jar: Right Click on your Project -> Properties -> Java Build Path -> Libraries -> Add Jars..
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 interested in making a one-click installer for my C# application.
I had the the framework of the application down. The logic of the application in the installer() method was:
public static void installer(){
deleteLegacyFiles(); // deletes old files through a find method
moveSQLite(); // moves the database file
if(checkRevit2013()){ // checks whether Revit '13 is installed
movePlugin2013(); // moves my plugin into the Addin folder or Revit
}else if(checkRevit2014()){ // check whether Revit '14 is installed
movePlugin2014(); // moves my plugin into the Addin folder or Revit
}else{
System.out.println("It does not look like you have either Revit 2013 or Revit 2014 installed.");
}
}
However, this Java script (not Javascript, but a Java script) really only took three folders from the /Desktop/ and copies them to their respective target folders. I am interested in a solution that turns all my three folders into one executable file (something like an .exe or .msi) and do the above actions.
Are there any solutions for this for Java? Something that packages multiple folders/files together and then allows for one-click solutions for installation? I'm not exactly how to phrase what I want, as this is my first software development project. Any suggestions are welcome.
You can create a single executable jar file in java. This jar would have an application that does all the copying you've listed above. But instead of copying from the desktop, it would copy directories that are included in the executable jar. A jar is a zipped file type (in fact you can change the extension from jar to zip and examine the contents).
Your strategy will be to create a regular java application, package as an executable jar. Include the directories you want to install as resources in the jar. Check out the jar documentation for all java utility methods and classes to manipulate jars.
http://docs.oracle.com/javase/tutorial/deployment/jar/
Are you looking for making/building an executable jar file? If so you can use something like one-jar.
http://one-jar.sourceforge.net/index.php?page=introduction&file=intro
Here are the steps:
Create an executable JAR file with your application's CLASS files in it. (Navigate to bin directory of workspace) Name this "main.jar"
jar cfm main.jar manifest.txt *.class OR [jar cfm main.jar manifest.txt .]
Create three directories: MAIN, LIB, and BOOT
Place your "main.jar" file in the MAIN directory.
Place the jar files that your main application depends on in the LIB directory.
Naigate to Packaging- Create a new JAR file out of the MAIN and LIB directories. Name this one "MyUtil.jar". You do not need to add a manifest or do anything special to this file. It does not need to be executable. Just make it so that it contains the contents of the MAIN and LIB directories.
jar cf MyUtil.jar main lib
Extract the contents of the "one-jar-boot.jar" file into the BOOT directory.
Navigate to the BOOT directory, and update the "MyUtil.jar" file with the following:
jar -uvfm ../MyUtil.jar boot-manifest.mf .
Your "MyUtil.jar" file should now be executable. Test it.