Quickest method to package a Java console app - java

I've got a very small set of classes built up in a custom package hierarchy with one console app class that employs them. Everything works fine from JCreator and from a command prompt.
I'd like to build a second console app that re-uses that same package.
As a Java newbie, what is the quickest, dirtiest method to do that?
My main concern was avoiding copying the package directories over to the new console app's directory.
Using JCreator, I didn't have any problems adding the package directory to the project and compiling and running. But when I tried to run the console app from the command line, it couldn't find the classes in the package hierarchy.
In Visual Studio, you just add a reference...

What you want to do for both apps is create a jar file with a Main-class definition in the var manifest. There's a good bit of information on this in the Java Tutorials, but the gist of it is just that you'll create a jar file with the jar tool, and then make a little wrapper to run it as
java -jar myfile.jar

If you do not wish to copy your class files from your first application, then you need to set up the classpath used when you run java from the command line to include the location of those files.
Make sure you also include the location of your newly created class files.

Using a tool like process exlporer, you should be able to see the command line which JCreator used to launch your application.
(Typically IDEs also include lots of command line parameters for being able to connect to your application and debug it, etc.)

If I read this correctly, you have a class I'll call A that references packages in package pkg and now you wish to create a new class B that also uses the classes in pkg.
One option is to create a pkg.jar file that contains all the classes in pkg and then create separate jar files to hold A and B. In the manifest files for A.jar and B.jar you can include a Class-Path element to include pkg.jar
Then so long as pkg.jar is sent along with A.jar or B.jar, they would each reference the pkg.jar without having to worry about specifying the classpath on the command line.
Some details here: http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html

Related

How to make VS Code recognize .class file?

I'm trying to use VS Code with java, but I have an issue.
As a student, my teacher gives me a file named Clavier.class.
I have already wrote a programm with another IDE, and when I execute it in the command of Windows, it works without any problem. But, when I put this programm in VS Code, it doesn't recognize Clavier.saisirInt, which is a static method in Clavier.class. I guess that VS Code can't use a class which is in a File.class, but is there a way to make it works ?
Here is an image of my workspace and you can see the error at the bottom
I assume your lib/ folder is placed onto the execution classpath. If this is the case, that's where compiled class files should go, not next to your sources
Otherwise, you'll need to inspect the java command that actually runs your code to see what/if the -cp argument is set to, or include those class files similar to how the documentation shows for JAR files (which are just zipped packages of class files, and honestly what the teacher should provide instead)
By default, VS Code will reference all JAR files in workspace's lib directory using the glob pattern lib/**/*.jar
And so, you need to adjust this to include lib/*.class after moving your files there

Javac does compiling always have to be from top of package? (Passing path of package to javac ?)

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.

Compiling and executing through commandLine shows NoClassDefFoundError when trying to find Java package

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:\..."

Associate Java class-files to run on double-click on Windows

If there's one thing that annoys me about Java it's that you can't double-click a class file so as to run. I assuming there's an entry in the registry that has to be edited to do this but I haven't a clue.
So, as it says on the tin. Does anyone know how to associate Java class files to run on double-click on Windows (I aiming for Windows 7 here but I'm sure there'd be no difference in the three most latest releases)? It would make my life (and I'm sure many other people's) much easier!
Udpate: I've seen answers relating to making a JAR out of the class in question and running it that way. However useful, that is not exactly what I'm looking for here. I'm effectively looking for Windows itself to invoke java with the class on double-click, with the correct arguments.
if classpath doesnt matter too much, easily done with a simple batch file runjava.bat or so that is associated with .class files in the explorer (via right click >> open with..)
#echo off
REM change to folder where the class file resides
cd %~d1%~p1
REM execute the class by calling its name without file extension
start java %~n1
The double-clickable JAR solution is the most common plain Java distribution method. There'd be a number of issues with trying to execute .class files directly, with the classpath the one that pops first to mind.
That said, if you wanted to support the very simplest possibilities in your development environment, you could conceivably implement a script that
inspected the .class file for the full class name (including package and inner class name)
walked up the directory tree to the root of the file's class path
(optionally included any common lib directories in the classpath)
invoked Java for the determined class
Then you could register your shiny script as a handler for .class files. But since you're in the development environment, aren't you happier with your IDE doing that?
For a .class file to run, needs in first place to have "something" to do, that is, that .class should contain a main method. Not all the .class do have one.
One thing you can do, is to wrap your app ( a number of .class files ) inside a jar file.
For short, you just need in addition to your classes a manifest file that says, where the main method is:
jar -cmf yourmanifestfile.mf doubleClickApp.jar *.class
And that's it, the doubleClickApp.jar is now executable with a "doubleClick" gesture.
When you install the Java Runtime Environment, it registers .jar files as an association in Windows. If you double-click on a .jar file, it will open it using Java. For this to work, you need to make sure you have a manifest defined that points to the class to run. Your class file to be run must have a main method that will be called.
Let's assume you have a class named 'com.TheClass.class' on disk. If you want to have this able to run with double click, create a file in a new directory called META-INF/manifest.mf. Put this into it:
Manifest-Version: 1.2
Main-Class: com.TheClass
Zip (or use the jar command) both your class up with this manifest directory and file. Rename it to mine.jar. Double click on it and it should launch your class with the Java runtime.
http://justaddhotwater.webs.com/javaexec.htm
This software makes it possible to run your Java classes by double-clicking them.(Windows ONLY).
The easiest way that I have found was creating a shortcut on the same folder than the .class file. Then right click on it and go to properties. Change the field Target to java NameOfClass, finally double click the shortcut :)

Eclipse - Compiling & executing program

In the past, I have used JCreator to develop my java applications. I wanted to try Eclipse and see its IDE.
On JCreator, I could just open a java file and run it in the command prompt. On Eclipse, I cannot find out how I can compile and run a java file written by someone else without first creating a project, creating a new java class and then copy and pasting the code from the file.
I cannot even figure out how to import a java file into the project.
So my questions:
1. How can I open, compile and run a java file without first creating a java project.
2. How can I import a java file into a project I already have.
3. How can I run the program in command prompt, instead of on the console that is in Eclipse, (update: to be more clear) directly from the Eclipse.
Thank you.
1/ Do not forget scrapbook page (also presented here)
In theory, you could copy a class in it, but since that class is actually encapsulated into a "main invisible" class, as an inner class, it would not support any static methods in it.
However, for a quick chunk of code, it does the trick just fine.
(from this Eclipse tutorial)
3/ Since you want it run directly from eclipse, use as indicated in this thread, an External tool
That External Tool would run C:\WINDOWS\system32\cmd.exe with working directory ${container_loc}
More details in this article "How do I open a Windows Command Prompt in my console ?".
(source: avajava.com)
That will give:
(source: avajava.com)
As far as I know, you can't
Right click project > Import
With respect to your platform, something roughly equivalent to: java <classname>. (Make sure java is in your path, otherwise use the absolute path to the it.)
A useful feature is available in Eclipse since version 3.4: you can paste the text of a Java class into a Java project (copy the content of the class, select the project or source folder, paste). This will create an appropriately named file, in the correct package. This is probably the easiest way to import a single class.
2) Use the "Import" command. Ensure the java file goes to the "src" directory.
3) One way is to export your classes to a JAR file and then run the JAR file at the command prompt (e.g. java -jar file.jar)

Categories

Resources