I'm trying to build executable jar file. I'm fairly new to this and not really sure how it is suppose to work. I'm using eclipse 64 bit and windows 7. I'm exporting the project to jar file. This is project code. when I double click the file it doesn't do anything and I'm expecting to see a message box. Can you please tell me what am I doing wrong and help me fix it. thanks.
import javax.swing.*;
public class Starter {
public static void main(String[] args)
{
String st="Welcome";
JOptionPane.showMessageDialog(null,st);
}
}
It sounds as if there is no Main-Class defined in the MANIFEST.MF in your jar file.
There are numerous guides on how to create an executable jar available such as this one.
Related
We have a java app that needs to use a public class form a JAR file. After much frustration with the main application, we have created a simple repo here to try to figure out what is going on.
The overly simple file that ends up in the JAR file is as follows:
package com.mystuff.helpers;
public class printStuff {
public void showMsg(String msg) {
System.out.println(msg);
}
}
We create the JAR file with this command:
jar cvf MyJavaHelpers.jar com
The folder structure is as follows (the printStuff.java file is in the helpers folder):
A listing of the JAR contents is as follows:
jar tf MyJavaHelpers.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/mystuff/
com/mystuff/helpers/
com/mystuff/helpers/printStuff.java
com/mystuff/helpers/README.md
Finally, the program that we have to use this simple class is as follows:
package com.mystuff.testapp;
import com.mystuff.helpers.*;
// To build the JAR file
// jar cvf MyJavaHelpers.jar com
// To display the contents of the JAR file
// jar tf MyJavaHelpers.jar
public class testDriver {
public static void main(String[] args) {
System.out.println("Starting testDriver");
com.mystuff.helpers.printStuff ps = new com.mystuff.helpers.printStuff();
// testPrintStuff(ps);
// testPrintStuffAgain(ps);
}
/*
private static void testPrintStuffAgain(printStuff ps) {
ps.showMsg("This is a fine kettle of clams");
}
private static void testPrintStuff(printStuff ps) {
ps.showMsg("This is a fine kettle of fish");
}
*/
}
In VS Code (v 1.55.0) We have a Java Project to contain our TestDriver that looks like this:
Finally, the issue is that when we try to run the test driver, we get the message:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
com.mystuff.helpers.printStuff cannot be resolved to a type
com.mystuff.helpers.printStuff cannot be resolved to a type
at com.mystuff.testapp.testDriver.main(testDriver.java:15)
We have tried the command Clean Java Language Server Workspace which seems to indicate that it works, but we cannot get past this error.
Based on what we have looked at, the JAR file appears to be in the correct place (It is in the lib folder of the main app).
The import com.mystuff,helpers.; line does not show as an error, so it seems to us that it is found, however, the actual import of the printStuff class fails. We have tried the fully qualified class name as well as relying on the import and only using the short name. They both fail.
We have seem some guidance about setting the classpath, but have not been able to find how to do that explicitly in VS Code.
Of course, if we do not have this little helper in a JAR file, but just as a side-by-side in the same project, it works just fine. The issue that started us down this journey is trying to use a public class from a pre-packaged JAR file.
Any and all help is greatly appreciated. Thanks in advance.
Before adding the jar to library, you may run the command java -jar printStuff.jar to test if it could be executed successfully.
The error occurs because the class must be called with its fully qualified name. To be clear, the name of this class is not printStuff, It's com.mystuff.helpers.printStuff, so the right command should be:
Turn to the folder;
Compile .java file: javac com\mystuff\helpers\printStuff.java
Generate .jar: jar cvfe printStuff.jar com.mystuff.helpers.printStuff .\
Then readd it to referenced libraries and see if the error goes away.
just started learning java, i'm looking to save a .java file (TextIO.java) into my working directory. (which file makes up my working directory exactly?)
and then run it through cmd so that Eclipse can use the class.
I tried doing that by saving (and running thru cmd) the file in the folder containing the programs (yah right) i have written already. But Eclipse still cant find the class.
Can i get some help with this?
thanks
You have to create an Eclipse Java project first. The you can copy the file from file system explorer window and paste it on the Eclipse Package Explorer tree. Eclipse projects compile all files in their source folders automatically by default.
The TextIO.java clas you mentioned has a javadoc saying: ... TextIO provides a set of static methods for reading and writing text... This means you use it as a toolkit rather than executing as an application. Given TextIO.java and you class TextIOUtilizer are both in the root of your classpath (source folder in Eclipse), here is how you call a TextIO method:
public class TextIOUtilizer {
/**
* #param args
*/
public static void main(String[] args) {
// code
TextIO.skipBlanks();
//code
}
}
Now you run TextIOUtilizer as a Java application - it has a main method. Hope this helps.
I'd like to use a Java library I wrote in an Android project. I did two things:
Add the jar to the build path.
In the package explorer now has a Referenced Library folder
Compile works
I copied the jar into a libs folder within the project
Compile works
But the program crashes during runtime for both approaches. The program does not recognize the class I am calling. A classDefNotFound is thrown.
I have tried this for various jar files. The ones I write do not work. While downloaded jars work. Perhaps there is something wrong with the classes I am writing?
Android Code:
package com.davidk.androidtest;
import com.davidk.libs.Math; //<--classDefNotFound is thrown
import com.parse.Parse; //<--works
import com.parse.ParseObject; //<--works
public void pressButtonOnClick(View v){
int answer = Math.add(1,1);
answerText.setText(answer);
}
public void pingParseButtonOnClick(View v){
ParseObject p = new ParseObject("Ping");
p.put("String", "ping");
p.saveInBackground();
}
davidk.libs.Math code:
package com.davidk.libs;
public class Math {
public static int add(int x, int y){
return x + y;
}
}
I guess you are using Eclipse. Do not try to mess with jars. The easiest way is :
In your library project, check the Is Library in the Android section of the project properties
In your dependant project, add the library in the same place (Android section). Do not add anything in the "Java build path" section ! (this is the most common mistake)
Each jar file exported contains a manifest file, to which the code tries to refer. In your case, I guess the manifest file is missing or corrupted. To generate the jar without any issues or complications, there are many open source tools available, which will help you to achieve this.
Other possible method is as suggested by #Orabig.
Please visit below link for more.
http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html
I tried to run the java program but missing packages.
import org.activiti.engine.*;
public class RawDriver
{
public static void main(String[] args) {
ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration().setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE).setJdbcUrl("jdbc:h2:mem:my-own-db;DB_CLOSE_DELAY=1000").setJobExecutorActivate(true).buildProcessEngine();
System.out.println("processEngine = " + processEngine);
}
}
How can i import external packages and run the program?
The external packages need to be in your classpath. This can be set as an environment variable or on the command line. You need to have the libraries associated with what you need which are usually shipped as jar files. More about the classpaths here: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
if the external packages are in another project, you can import that project, then in the project you are writing, choose Java build path -> project, add the project you just import. Then go to your java file and import the packages.
If you are using esclipse , if you have a jar file then you can add the jar file to you classpath using the project --->Properties-->java build path
add the jar in the project libraries , you will be able to access external packages like this .
if you using command line option or unix , set the class path using the command line option set CLASSPATH option.
Hope this helps
Abhi
(Using JDK7)
A friend of mine asked me to write him a program for rolling dice of arbitrary number and size. So I did. For simplicity's sake, I kept it as a console app. Now I'm trying to distribute it to him. I tried exporting it as a Runnable Jar with Eclipse's built-in exporter.
After opening it with Java (As opposed to javaw)... Nothing happens. I'm able to take a screenshot of the command prompt before it closes, and it reads:
Error: Could not find or load main class C:\Users\Matt\Desktop\Roller.jar
Contents of Roller.jar
META-INF/MANIFEST.MF
roller/Roller.class
Contents of MANIFEST.MF:
Manifest-Version: 1.0
Class-Path: .
Main-Class: roller.Roller
Contents of Roller.java:
public class Roller{
/* Irrelevant code expunged */
public static void main(String[] args){
//Irrelevant code expunged
}
}
I want to get this in a state where I can just email him the .jar and he can double-click it to run it. Can anyone help?
In Roller.java, you need to add package roller; to the top of your class to match Roller.class being in the roller folder in the JAR file. After this, you don't even need to specify a Class-Path in your Manifest file.