문제

While using jGRASP I noticed that the output for programs in packages was slightly different than normal, specifically the program names appeared to be relative paths starting from the top of the class.

For example, for a Java program that includes the statement package ch01.stacks;, the compile output looks like:

 javac -g ch01\stacks\ArrayStack.java

and the run output appearing similarly.

I was wondering if there was a relatively straightforward way to simulate this behavior in other programs such as Notepad++ or gedit where users can set up scripts to compile programs.

EDIT: I apologize, I forgot to mention that the compilation scripts I'm talking about are essentially the program filename passed to javac. I would rather not use absolute paths, I would like my scripts to work in a way similar to jGRASP if at all possible.

To further clarify the issue at hand, with my current scripts I believe the package structure is giving me issues, since it is compiling in the current directory of the program. I am looking for a way to relatively compile my Java programs with respect to package structure.

That is, is there any way to detect the top directory needed for the compile (ch01 in the previous example) without having to dig through the program looking for package?

도움이 되었습니까?

해결책

I'm still not sure what you want, and this wouldn't fit into a comment.

jGRASP's behavior is precisely normal behavior. Java class files, both the source, and compiled classes, live in a file hierarchy that mirrors their package names. Any Java IDE or command-line build tool (Ant, Maven, Gradle, whatever) understands this, and behaves accordingly.

If you genuinely wish to re-invent those wheels, your code needs to do the same. If your Java source seriously doesn't live in the canonical package/file hierarchy, then yes, you'll need to interrogate the source for each file's package declaration, and put the compiled .class file into the appropriate directory.

javac's -d option sets the output directory. Compiled classes will be placed in their correct location based on their package name. However when compiling, all imports must be available on the classpath, whether your own classes, or those of a third-party library (generally in a jar).

In short: do not do what you're asking about. I can think of no good reason to do so. (This doesn't mean there aren't any good reasons, but I'm... highly skeptical.) It's even possible to use relatively-canned make files to build Java projects; a better solution that what you're proposing, but still a horrible idea.

Note: When you run a Java application, the class files must be present in the expected hierarchy, whether packaged in a jar (or war) file, or on the filesystem.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top