I'd like to make "double-click" cli application but still don't get how.
I know I should propably somehow edit manifest but that is all. I googled ofc. but no success.
Thanks for any tips.
Here is the output from build, run, and manifest:
compile:
Created dir: /home/nick/NetBeansProjects/SemestralWork/dist
Building jar: /home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar
Not copying the libraries.
To run this application from the command line without Ant, try:
java -jar "/home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar"
jar:
BUILD SUCCESSFUL (total time: 1 second)
java -jar /home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar
Exception in thread "main" java.lang.NoClassDefFoundError: semestralwork/Main
Caused by: java.lang.ClassNotFoundException: semestralwork.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Could not find the main class: semestralwork.Main. Program will exit.
MY MANIFEST created with build:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 14.0-b08 (Sun Microsystems Inc.)
Main-Class: semestralwork.Main
Class-Path:
X-COMMENT: Main-Class will be added automatically by build
These two lines tell you all you need to know:
Exception in thread "main" java.lang.NoClassDefFoundError: semestralwork/Main
Caused by: java.lang.ClassNotFoundException: semestralwork.Main
And a further clue is dropped by the manifest output:
Main-Class: semestralwork.Main
This means that the JAR file is looking for a package named semestralwork and a class named Main inside it. It fails at this point because it cannot find either the semestralwork package or the Main class.
As you pointed out in your question, the problem is indeed in the manifest file. You can edit this directly in your JAR file if you like, but a better idea would be to do this from Netbeans:
Click on `File --> Project Properties (semestralwork)'
In the dialog that opens, on the tree on the left select Run
Then, on the right, under the field labeled Main class:, enter the fully qualified class name of the class that you want executed when run from the command line.
In your case, as I see from your comment on #Aaron's answer, if your main class is in a file called encryption.java, and it is in the default package (no package), just enter encryption.
Once this is done, do a clean and build, then try running it from the command line again.
HTH
Since I encountered the same problem, I can clarify the solution a little bit.
You must create main Java class outside your method (for example-default_package folder), and then invoke your method(folder), e.g import your_folder.connected_class; in that main class.
Hopefully I could help someone with the same problem.
It's easier to make a .exe from a .jar without netbeans.
Here are my suggestions:
1. Use a special application for this(ex: JSmooth, JEXECreator etc)
2. Make a C++ program that starts a JVM (see this tutorial)
The default class search path may be the issue. You should try changing directory to the location the jar is and launching with java -jar Semestral.jar. Also you may have misnamed the main class. Please also include your package structure.
Open the JAR file with a ZIP tool (or try less ... if you're on Linux or jar tvf ...). Make sure there is a directory semestralwork in there which contains a file Main.class.
It's Easy. Download a copy of netbeans. Make a new project in netbeans. Goto your main class in the Projects Explorer. This should be folder "YourProject" As the class YOURPROJECT.JAVA . It is this yourproject.java file that you want to start with. Just write your code into the public static void main area. You can run the program with the green play button in the top toolbar.
Related
I'm new to using jar files on my applications so here is my problem.
I wrote my code on net beans and added into the library the jar file I need, which is:
poi-3.10-FINAL. The program runs perfectly from net-beans, however when i try to run it from the command line seems like it doesn't find some of the files inside the jar. Reason for this I would like to make it an executable after i get this solved.
In the command line I'm compiling my code as follows:
C:\Users\chuser10\Desktop\Excel\src\excel>javac *.java -cp C:\Users\chuser10\Des
ktop\Excel\src\excel\lib\poi-3.10-FINAL.jar
It compiles perfectly, which lead me to think everything is good to go, however this is not so. I tried then running my main as ...>java GUI and i got this:
C:\Users\chuser10\Desktop\Excel\src\excel>java GUI
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apach
e/poi/poifs/filesystem/POIFSFileSystem
I checked inside the jar and the file is there. Any clue why this might be?
You need to specify the classpath when you run the program too. Compiling doesn't link the library into your code as happens in C and many other languages; in Java linking happens at runtime.
Probably -cp .;C:\Users\chuser10\Desktop\Excel\src\excel\lib\poi-3.10-FINAL.jar will be what you need. The '.' at the beginning means the current directory, which is where the class files that constitute your program are rooted. The ';' is just a separator.
Got it working. You are both correct, we have to specify the class-path at runtime as well.
I went ahead and went for the executable jar file creation and put it on my manifest:
..>jar cfm < *.class>
On my manifest:
Class-Path: poi-3.10-FINAL.jar
Main-class: GUI
Cheers!
In Eclipse Indigo (on Mac OSX), I have a project 'Test'. In this project I have a package 'test1'. In this package, I have a class Test.java.
In short the file structure is: Test/src/Test.java and
Test/bin/Test.class
I have a library mylib.jar in lib folder.
I also have a data file named info.dat. In info.dat, there's information about which class to load. For example, in info.dat, I specified: class=Test
then, when I run:
$ java -jar lib/mylib.jar info.dat
(NOTE: this works if I run this command in terminal and have class file in the same folder with info.dat)
the main function in mylib.jar will load Test.class.
I'm using External Tools to achieve this. Here is my External tool setup:
Location: /usr/bin/java
Working Location: /path/to/Test_folder
Argument: -cp . -jar lib/mylib.jar bin/test1/info.dat
However, when I try to run, I get this error:
LOADING INFO FROM FILE bin/test1/info.dat
=> Current directory=[not include because not important]
=> Full pathname=[not include because not important]
java.lang.ClassNotFoundException: Test
I also tried to change class=test.Test in info.dat but got a similar error
java.lang.ClassNotFoundException: test1.Test
The result shows that the info.dat was loaded ok, but the path to class file was not correct. How should I configure the external tool to do what I need? Can someone please give me some instruction?
Many thanks,
It was an easy solution with Run Configuration. I just need to pick the main class from mylib.jar in Java Application and put info.dat in the argument field.
Sorry for wasting the resource here.
Luckily or unluckily, I haven't had to work too extensively with invoking java from the commandline up until this point, I've usually been using something like Maven, Ant, or running things within a servlet container. I've just compiled my application in Maven into one JAR using the assembly:single goal, and am having serious problems running it from the commandline.
Here's what I'm attempting to do:
export JAVA_CLASSPATH="`pwd`:/path/to/remote/libs/"
java -cp "${JAVA_CLASSPATH}" -jar groupId-artifactId-version-jar-with-dependencies.jar com.my.main.Class
This is failing with the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/remote/lib/IServer
Caused by: java.lang.ClassNotFoundException: com.remote.lib.IServer
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: com.my.main.Class. Program will exit.
The IServer class is in the /path/to/remote/libs directory and isn't being found. It also seems to not be able to find the main class, which is really odd. What am I doing wrong?
The -jar option ignores the classpath ( and all other ) options.
You need to edit the manifest file in your jar and set the Class-path variable in there.
http://download.oracle.com/javase/1.4.2/docs/tooldocs/linux/java.html
#Kal's answer is correct, but an easier way to fix it would be this:
export JAVA_CLASSPATH="groupId-artifactId-version-jar-with-depencies.jar:`pwd`:/path/to/remote/libs/iserver.jar"
java -cp "${JAVA_CLASSPATH}" com.my.main.Class
No need to tinker with the manifest file; just add the jar you want to the classpath.
Also, if the IServer stuff is in a jar inside /path/to/remove/libs/ then you need to explicitly include the name of the jar file. I've included it above and assumed it is called "iserver.jar"
If you have many dependencies then it's often easier to write a shell script for launching your code. The script can build a (potentially very large) classpath string using shell glob goodness. The java executable doesn't do any globbing.
EDIT: Note that I've assumed that you don't have access to Ant or Maven or any of that good stuff, since you stated that you usually use those tools but are not doing it right now.
The classpath parameter must be specified with the exact path to the jar file. If your IServer class is found in server.jar, you need to specify "$pwd\:/path/to/remote/libs/server.jar", for example. (in other words, you need to include a :-separated list of paths to jar files)
As others have noted, if you choose this method you should remove the -jar parameter and instead include the class name you intend to be your main class on the command line, such as java -cp $PWD:/path/to/server.jar com.remote.lib.IServer.
Side note: usually names that start with I are reserved for interfaces; it seems a bit odd to use an interface as your main class.
I'm attempting to restructure a legacy application and reduce the steps needed to deploy.
To do this, I'm packaging all the class files, and Resources into a jar file.
I'm using this command to jar everything:
jar -cfm ../Deploy/JEmu.jar Manifest.txt *.class Resources/
My manifest file looks like this:
Manifest-Version: 1.0
Created-By: 1.2.2 (Sun Microsystems Inc.)
Main-Class: JEmu
Name: JEmu.class
The class that is the entry point is JEmu.class, which is packaged in the jar, but when I run the jar I get this error:
java -jar JEmu.jar
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ControlBar.<init>(ControlBar.java:88)
at JEmu.<clinit>(JEmu.java:85)
Could not find the main class: JEmu. Program will exit.
I'm not exactly sure what is causing that.
Line 88 of ControlBar is:
stopButton = new JButton( new ImageIcon( classLoader.getResource("Resources/stop.gif")));
What am I doing wrong, it all works when it's not packaged into a jar?
classLoader.getResource("Resources/stop.gif") probably cannot find the specified resource. When this happens, it returns null, hence the NullPointerException.
The problem is that the VM is not able to find the resource named "Resources/stop.gif". The reason for this is that the stop.gif file is located in the package "Resources" as it is in the base of the JAR file. By default, the getResource method will start with the package of the class from which the classloader was retrieved. In the case of your code it would be the package in which the Thread class is located. The end result would be that the VM looks for the image in the location "/java/lang/Resources/stop.gif", which is not where your image is located.
If you are looking for a fixed location, i.e., Resource directory located in base of JAR, then make sure to prefix your URL string with '/' as in:
classLoader.getResource("/Resources/stop.gif");
Another thing to think about is whether you need to use the ClassLoader from the current Thread. Typically the ClassLoader used by your application class would be more appropriate. You can retrieve that by executing something similar to this.getClass().getResource(...).
I am trying to run a file from command line. The file is a .class file and is apart of a larger project that I compiled in Netbeans. I navigated to the .class file and ran
java MyFile
And I got:
Exception in thread "main" java.lang.NoClassDefFoundError: PersonTest/class
Caused by: java.lang.ClassNotFoundException: PersonTest.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: PersonTest.class. Program will exit
Whats up with that? (I should mention that i'm running ubuntu)
You need to check this useful link java - the Java application launcher:
By default, the first non-option
argument is the name of the class to
be invoked. A fully-qualified class
name should be used
So, you have to write the full qualified name of the class (this includes the package name).
So, the right way to execute your command is this (from the root dir where your class files are stored):
> java my.package.MyFile
Also, make sure to include all the needed dependencies at the classpath (-cp) argument (check the referenced link).
UPDATE: to include a classpath setting example:
java -classpath C:\MyProject\classes;C:\MyProject\lib\utility.jar my.package.MyFile
With this, the java runtime will search for the classes at the C:\MyProject\classes directory, and at the C:\MyProject\lib\utility.jar JAR file. You'll need not only your class direct dependencies, but the dependencies needed by the referenced files (the whole tree).
The answer appears to be in this line:
Exception in thread "main" java.lang.NoClassDefFoundError: PersonTest/class
It means you didn't type:
java MyFile
as you said in your original post, you typed
java PersonTest.class
you should have typed
java PersonTest
Yes you can, they are compiled by a java compiler. If you have the right version of the jvm (often other versions work aswell) than it can be run. The information about your error is not enough to tell what went wrong.
Your probably in the wrong folder, mistyped the classname, used a class in your code that couldn't be found, etc.
Unless your class is entirely standalone (i.e. only references java.lang classes like String), you'll need to add other classes/JARs to the classpath when you invoke Java.
The NoClassDefFoundError (which usually states the name of the class by the way, and always includes a stacktrace) indicates that an external class that was available when your class was compiled, is not available on the classpath at runtime.
EDIT based on update:
You're invoking your process incorrectly. You don't need to append the .class suffix of the file - doing so makes Java look for a file class class in a subpackage.
(P.S. you said you ran java MyFile. That's a lie, you actually ran java PersonTest.class. If you'd noted that to start with, it would have made it much easier for people to answer the question!)
Just consider this example
say I already have a folder src and I wrote in my notepad
package test.oye;
class testclass {
static public void main (String [] args)
{
int a=3;
System.out.println(a);
}
}
then what go to src folder and you ,yourself create a folder named test and inside it oye . Then put your .java file in it . Then cd src/test/oye only(in Command prompt or terminal).From there itself
javac testclass.java
cd src
java test.oye.testclass
This will work for sure.
If you don’t want to put .java file there … then just compile your .java file and get the .class file .
Now create the test folder and then oye inside it ….and put .class file inside it ….
Now go back to src …and then type
java test.oye.testclass;
according to terminal ide, android requires classes in DEX format when running them.
Try:
dx --dex --output=practice.jar practice.class
Then run using this:
java -jar practice.jar practice