Re: jar, package and import relationship?



Dan Stromberg wrote:

sys.path.insert(0, '/my/directory')

This I don't really like. If you ever change the location of /my/directory, you have to edit each source file where this appears and alter the path. Maybe you've got a config file some place that you'd use in real production code, but still it feels a bit ugly.


1) /my/directory/interface.h has simply:
void hello_world();

In Java, the .class files themselves have this information.

2) implementation.c (in the current working directory) would look

This would be equivalent to Implementation.java.

like:
#include <stdio.h>

Note that import and #include do different things. However both require that you have a properly configured environment. #include requires a correct INCLUDE path. Java does too: the CLASSPATH must be correct. It's a bit different for anything that comes with the default with Java, because the JVM handles those a bit differently with out environment variables, but for any library (.jar) or .class file you download or create, you need to tell your tools (IDE, javac compiler, etc.) where to find them.

This is no different from C here. Totally the same idea.


void hello_world()
{
printf("hello world\n");
}

In file Hello_World.java:

public class Hello_World {
public static void hello_world() {
System.out.println("hello world");
}
}

3) ...and the main program (again in the CWD) would look like:
#include "interface.h"

int main()
{
hello_world();
return 0;
}

File Main.java:

public class Main {
public static void main(String ... args) {
Hello_World.hello_world();
}
}

4) And a basic Makefile (again in the CWD) for this would look
like:

Don't forget that C also requires your LIBPATH to be set correctly, for any libraries which load at runtime. For Java, this is the same with respect to any loose .class files that the runtime needs to find, or any ..jar files that you use. CLASSPATH is used for this as well.

(Remember that .jar files and .class files take the place of .h files in C., so really it makes sense to just configure one CLASSPATH variable rather than have one compile time include path and one separate LIBPATH at runtime.)

From the command line:

Brenden@Homer ~/Dev/misc/hwtest/src
$ ls
Hello_World.java Main.java

Brenden@Homer ~/Dev/misc/hwtest/src
$ javac Hello_World.java

Brenden@Homer ~/Dev/misc/hwtest/src
$ javac Main.java

Brenden@Homer ~/Dev/misc/hwtest/src
$ ls
Hello_World.class Hello_World.java Main.class Main.java

Brenden@Homer ~/Dev/misc/hwtest/src
$ java -cp . Main
hello world

Brenden@Homer ~/Dev/misc/hwtest/src
$ java Main
hello world

Brenden@Homer ~/Dev/misc/hwtest/src
$ echo $CLASSPATH
..;C:\Program Files\Java\jre1.6.0_03\lib\ext\QTJava.zip



Note that the last two runs are equivalent because I have "." in my classpath. Also, I haven't used packages, which is normally a bad idea. But it's enough for this very simple example.
.



Relevant Pages

  • Re: Java ptolemy plot package.
    ... By messing around the CLASSPATH variable I got one of the compiler errors ... It knows where the package is, ... class file contains wrong class: ... Java breaks up classes into "packages" - groups of related classes. ...
    (comp.lang.java.programmer)
  • Re: Java ptolemy plot package.
    ... By messing around the CLASSPATH variable I got one of the compiler errors to go away. ... It knows where the package is, but I still can't instantiate the class. ... class file contains wrong class: ... Welcome to the wonderful world of classpaths, packages and directories, which drove me away from Java the first time I tried to use it. ...
    (comp.lang.java.programmer)
  • Re: javac -J?
    ... But that has very little to do with Java. ... where the constants in Compiletime were specified either automatically, ... into the .class file so that .class files created at different times (but part ...
    (comp.lang.java.programmer)
  • Re: run any location?
    ... java -cp c:\javaprograms helloWorld ... As long as the location of your class file is in the classpath it should ...
    (comp.lang.java.help)
  • Code Cracking in Java
    ... Software developers, designers, security professional, auditors, managers responsible for evaluating products. ... I love programming in Java but unfortunately I didn’t get a chance to work in software development firm but I am very much happy with my profile, basically I am an ethical hacker currently working in Wipro and previously in PwC. ... For security reasons I will not mention client name, application vendor and detail about the application architecture. ... As we commonly know that the basic loophole in Java is that it can be reengineered from class file to source code, the class file consist of byte codes which is interpreted by Java Virtual Machine to make Java platform independent. ...
    (Pen-Test)