How to create a executable jar file without any GUI component? - java

I have just created a program that uses file I/O and for interactive input and output it uses terminal window of IDE. I want to create a executable jar file of this program. Is it possible? If yes, then how?
I am using BlueJ as IDE.
The program have **no GUI component**
I have created a jar using BlueJ but it does not works.. When I double click on jar nothing happens...

You have to have an Main-Class entry in your Manifest. See documentation for details.
Given yours example
public class MainClass {
public static void main(String agrs[]) {
System.out.println("magic");
MainClass.interning_behaviour("12");
}
public static void interning_behaviour(String string_input) {
String string="12";
String string_5;
string_5=string_input.substring(0);
System.out.println(string==string_5);
}
}
1) You have to compile it: javac MainClass.java
2) You have to jar it jar cfe MainClass.jar MainClass MainClass.class
3) Now you can run it java -jar MainClass.jar

basically you should type:
jar cf jar-file input-file(s)
You can find usage details at link below:
http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

It depends on the IDE you're using.
In eclipse for example you can do this by right-clicking the package in package explorer window and navigating Export ->Java -> JAR file.

Export as a runnable jar if you're in eclipse

Related

IntelliJ: Why can't I use a class from external jar

When I created the jar file I wrote the following java file:
package myjar;
public class MyClass {
public static void hello() {
System.out.println("hello");
}
public static void main(String[] args) {
MyClass.hello();
}
}
I named the file MyClass.java.
I created a class file by typing "javac src/main/java/myjar/MyClass.java"
I generated a jar file by the command:
"jar cvf myjar.jar src/main/java/myjar/MyClass.class"
Now, I took the jar file and added it to Project Structure. The name of the jar is 'myjar':
and in the IDE I wrote "import myjar.MyClass" and the word 'myjar' was marked in red.
When I use 'MyClass.hello()' in the project, I get an error:
no main manifest
Do you know what I can to to load it successfully ?
You can only import some classes from your libraries on classpath into a Java source file, not a contents of a whole Java archive (JAR) file. So if you have inside myjar.jar file class Foo.class inside package (folder) bar, you can do
import bar.Foo;
After adding the .jar as a library in IntelliJ, you still need to add it to the module.
Then the library will appear in the module's list of dependencies.

eclipse cannot find main method from given launch configuration

I went to eclipse>export>java>runnable jar file
I changed launch configuration to my project name; destination: desktop.
I tried all three library handling options:
extract required folders into generated JAR
Package required libraries into generated JAR
copy required files into a sub-folder next to the generated JAR.
I did not check anything to do with "ANT scripts".
When I pressed "finish" in the export window, eclipse gave me a error: "Could not find main method from given launch configuration". After pressing the "finish" button in the eclipse export window, the next button was grayed out. It created a JAR file at the destination. When I tried to open it Mac's Jar Launcher (default)(13.5.0), it gave me the message:
The Java JAR file "filename.jar" could not be launched.
Check the Console for possible error messages.
I googled this and some people said to check the manifest.mf file. I extracted it, then: "META-INF>manifest.mf". I opened manifest.mf with textedit.
Inside manifest.mf were the lines:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ acm.jar
Class-Path: .
I am running Mac OSx 10.6.8 snow leopard. I have the latest java available for snow leopard (I think my java is the one just BEFORE java 7).
Also, I'm trying to export a project with:
a .java file that contains all my code
acm.jar
java.policy.applet
Scores.txt
My .java file contains a public void run() {} and JFrames/JPanels. My .java file extends ConsoleProgram.
If there's no public static void main(String[] args) method, the virtual machine (Java) will not know where to start your program. It sounds like your run() method is what you want to start your program. If so, then add (to your main class (.java)) a method: public static void main(String[] args). Re-export your .jar, making sure the launch configuration is set to your Project name, and your main class.

Java: Adding included Jar files to a created Jar file in linux

I need help including imported jar files into my java program in Linux. Here is the program:
import java.sql.*;
public class CreateCoffees
{
public static void main(String args[])
{
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
System.exit(1);
}
}
}
In order to execute Class.forName("com.ibm.db2.jcc.DB2Driver"); I need two .jar files added into the classpath:
db2jcc_license_cu.jar
db2jcc4.jar
I put these jar files into the same directory as my CreateCoffees.java file, then compile and run it like this:
javac CreateCoffees.java
java CreateCoffees
But I got this error
ClassNotFoundException: com.ibm.db2.jcc.DB2Driver
Then I tried the "-classpath" option
javac -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar CreateCoffees.java
java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar CreateCoffees
but got this
Exception in thread "main" java.lang.NoClassDefFoundError: CreateCoffees
Caused by: java.lang.ClassNotFoundException: CreateCoffees
How to I include those jar files into a my runnable jar so I can run it with java -jar myjar.jar ?
Try this
java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar:. CreateCoffees
when you use -classpath it looses current directory from classpath so it needs . in classpath as well explicitly
How to include the jars of your project into your runnable jar:
I'll walk you through it step by step with Eclipse Version: 3.7.2 running on Ubuntu 12.10. I'll also show you how to make the build.xml so you can do the ant jar from command line and create your jar with other imported jars extracted into it.
Basically you ask Eclipse to construct the build.xml that imports your libraries into your jar for you.
Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class: Runner Put this code in there.
Now include the mysql-connector-java-5.1.28-bin.jar from Oracle which enables us to write Java to connect to the MySQL database. Do this by right clicking the project -> properties -> java build path -> Add External Jar -> pick mysql-connector-java-5.1.28-bin.jar.
Run the program within eclipse, it should run, and tell you that the username/password is invalid which means Eclipse is properly configured with the jar.
In Eclipse go to File -> Export -> Java -> Runnable Jar File. You will see this dialog:
Make sure to set up the 'save as ant script' checkbox. That is what makes it so you can use the commandline to do an ant jar later.
Then go to the terminal and look at the ant script:
So you see, I ran the jar and it didn't error out because it found the included mysql-connector-java-5.1.28-bin.jar embedded inside Hello.jar.
Look inside Hello.jar: vi Hello.jar and you will see many references to com/mysql/jdbc/stuff.class
To do ant jar on the commandline to do all this automatically: Rename buildant.xml to build.xml, and change the target name from create_run_jar to jar.
Then, from within MyProject you type ant jar and boom. You've got your jar inside MyProject. And you can invoke it using java -jar Hello.jar and it all works.

exporting java jar help from Eclipse

so I have a project that only has one class Main:
public class Main{
public static void main(String[] args) {
System.out.println("asdf");
System.out.println(args[0]);
System.out.println(Integer.parseInt(args[1]));
}
}
so I tried to export this as a standalone jar...here's what I did
created a run configuration for the project...the run configuration runs properly from eclipse...it prints the arguments just fine...
right click on the project, selected export
choose the runnable jar file option
selected my previously created run configuration as the run configuration, chose export destination press finish, etc
but then when I run the program from the commandline:
Main.jar "testtest" 123
nothing came up.....it didn't print out the arguments....nor did it print out the "asdf" I specified in the main() function...
what did I do wrong?
Run java -jar Main.jar "testtest" 123 ?
I think you are getting wrong what exporting as a runnable jar means: This does not create an executable file. It just creates an archived file with all your compiled classes and some special meta information about which class to run when invoked with java -jar JARNAME.
Note that this also means all users of your program still need to have a java runtime installed.

How can I create a Class file and JAR file without main function?

How can I create the class file and jar file for this coding, when I compile this program its not working because there is no main function in the program. And also I am trying in command prompt but I don't know how to set the classpath? please help me
My Coding is here
public class NewLogFields implements ILogNotify
{
public void onLog(Level level, String comment, IMediaStream stream, String category,String event, int status, String context) {
if (category.equals(WMSLoggerIDs.CAT_session) && event.equals(WMSLoggerIDs.EVT_destroy))
{
Long csBytes = (Long)WMSLoggerFactory.getGlobalLogValue(WMSLogger IDs.FD_cs_bytes);
Long scBytes = (Long)WMSLoggerFactory.getGlobalLogValue(WMSLogger IDs.FD_sc_bytes);
System.out.println("disconnect: csBytes:"+csBytes+" scBytes:"+scBytes);
}
}
}
Once you compiled the coding in wowza media Serever the jar file is automatically created in the library folder,see your Installation Library folder.Still you have problem Gothrough this link Wowza Quick Guide
What do you want to do?
Create a class and an jar file out of this Java code so that you can use this in another Java program?
Then you have to compile it:
java NewLogFields.java
Looks like you are unable to compile it at all. This could be because the interface ILogNotify (or the jar that contains this) is not in the classpath.
You can include the path/jar containing this interface in the classpath by using:
javac -cp .;path_to_jar_or_class NewLogFields.java
where path_to_jar_or_class is the path to the folder or jar file that contains ILogNotify.
For example, this may be something like ./logNotify.jar or ./log/
You can set use switch -cp or -classpath with javac command.
for example javac -cp path and name of jat file or class file yourjavafile.java
create the class file using the compiler: javac NewLogFileds.java
create the jar file using the jar command: jar cvf stuff.jar NewLogFileds.class
You are correct that the program needs a main() function in order to run.
add:
public static void main(String args[]) {
// code here
}
With that you could run the code with or without the jar:
java NewLogFields
or
java -cp stuff.jar NewLogFields
There are ways of creating a MANIFEST file that tells java which class to run from the jar making the last line more simple.
The link that you provided tells you how to do it:
Compile your class in the normal way.
Create a JAR file containing your class in the normal way.
Copy the JAR file into the wonza installation's lib as described in the javadoc.
Edit the startup script to add the -Dcom.wowza.wms.logging.LogNotify=... option to JAVA_OPTS ... as described in the javadoc.
The "full-path-to-your-ILogNotify-class" is actually supposed to be the fully qualified class name of your class; it is obvious from the examples.
Edit WowzaMediaServerPro-Service.conf and log4j.properties as described in the javadoc.
Restart the server.
If you put your JAR file in the directory like the instructions tell you to, you won't need to modify the classpath by changing -cp argument.
Your class doesn't need a main method because it is not a free-standing application. Rather, it is a "plugin" class that gets dynamically loaded and instantiated by the Wowza core as required. The "-D..." option and the config file change tell the Wonza core which class to try to load.

Categories

Resources