Launch application Java - java

I made an application using bluetooth and so I had to use external libraries (bluecove). I am now looking to create a single executable to be able to give it to testers. I tested the executable JAR and it does not work, so I flip on a basic script.
I created a folder with all the java files inside and my external library and a folder with my pictures in it.
when I compile I have no error:
javac-cp bluecove-2.1.0.jar *. java
But in execution:
java -cp bluecove-2.1.0.jar Main
it gives me "error: Could not find or load main class Main"! I'm sur that my class contain my Main function Main.
What strange me is that in eclipse everything goes perfectly but not outside.
All those who have an idea and a hand to me is to thank in advance;)

java -cp "bluecove-2.1.0.jar;." Main
linux:
java -cp "bluecove-2.1.0.jar:." Main
Note the . which indicates the current directory, it must be also on the classpath so that your class Main can be loaded.

Related

java Could not find or load main class from eclipse compiled code

I have compiled my java code using eclipse but not it has to be deployed and a cron job has to execute it. I am trying to execute it from command line in Windows, but getting Could not find or load main class. I tried setting classpath using java -cp bin\com\pega\download\engineclasses but it still throws the same error. My folder structure looks like below
C:\Users\s2517457\G360_Linux\FiddlingPega
|__\bin\com\pega\download\engineclasses\TestUtils.class
|__\src\com\pega\download\engineclasses\TestUtils.java
Please let me know what should be the javac and java commands for this to work.
You should use the following command:
java -cp bin/ com.pega.download.engineclasses.TestUtils
Your are telling to java that the entire bin folder is your classpath and the main class is in the class com.pega.download.engineclasses.TestUtils
If you want to add jars as well, you must call the command like:
java --classpath "bin/;lib/*" com.pega.download.engineclasses.TestUtils
Where lib is the folder containing the Jars files

Is It necessary to compile entire program from an IDE like IntelliJ to run from command line?

Say I setup a program in intelliJ in java and I have multiple classes and everything setup. I am trying to run this program from the command line, but I keep seeing in tutorials they are using the command javac programname.java and they are compiling the program and then running it.
But I have multiple classes and I'm using intelliJ to do everything. Do I just compile it from intellij or what's the best way to do this? Do I even need to compile it, or is it all ready compiled?
I just want to be able to run my main class from the command line.
You do not need to compile it.
Just run your main class and IDE will do the rest.
Do remember to add required parameters to you main program, if any.
This link would be helpful https://www.jetbrains.com/idea/help/running-applications.html
You should think of the main method() as the entry point in your program. In other words, main() is the method that starts your program. So, when you are adding other classes intellij just adds imports to your other classes just like when your importing from the api. So, assuming that your imports are correct you need to take the following steps.
Compile all of your .java files (including the file with main which is sometimes referred to as a test client) using javac myFile.java
Run your compiled, main class with main method() by using java myClass
Edit: you must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath. Note that the windows classpath separator is a semi-colon ie ;
Then you can use java -cp to compile and run
javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main
You can make an executable jar :
More information : http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
Intellij : How to build jars from IntelliJ properly?

Cannot find main class, java error

I have been testing a java swing program that I have been making. On my one computer, the one I originally made it on, it works fine. I have tested the same program on 3 different computers and it runs when I launch it out of the ide, but when I double click the jar I get a popup error window titled 'Java Virtual machien Launcher'. The error is "Could not find the main class: xxxxxxx. Program will exit."
I cannot figure out for the life of me what is going on. It was working before.
You need to include a Manifest file within your jar. In this, you specify which class is to be used as the entry point when the jar gets launched.
Create a file called Manifest.txt, and add:
Main-Class: yourMainClass.class
Then, to create the jar :
jar cfm JarName.jar Manifest.txt yourMainClass/*.class
To run the from the command line, use :
java -jar JarName.jar

Eclipse Swing to JavaC?

I made a program using Eclipse, but I want to learn to compile it with JavaC.
I downloaded the JavaSDK jdk1.7.0 and installed it at C:\
My .java file is in C:\java
I then compiled the program using C:\jdk1.7.0\bin\javac Game.java
It uses a package layout manager so I downloaded swing-layout-1.0.1.jar and just put it straight into C:\ then tried to execute using:
java -classpath C:\swing-layout-1.0.1.jar Game
All I get is Error:Could not find or load main class Game
Any idea what I'm doing wrong?
You forgot to add your code to classpath. You should provide either jar file or directory where your classes are located, i.e.
java -classpath C:\swing-layout-1.0.1.jar;myapp.jar Game
or
java -classpath C:\swing-layout-1.0.1.jar;c:\proj\myapp\classes Game
If you know the name of your main class you can try
java -classpath .;C:\swing-layout-1.0.1.jar; your.package.MainClass

How to launch java application without having to specify classpath?

I'm new to java and I'm experimenting with hello world app.
I've exported the app from Eclipse into a jar, i DID specify the launch configuration, and when I ran it from command line, I retrieved
Error: Could not find or load main class
That can be fixed by specifying class path like this:
java -cp .:myjar.jar MyMainClass
However, this is really inconvenient. Is there any way, preferably through eclipse, to specify MyMainClass as a metadata inside the jar file so I don't have to write it down every time I launch the app?
yes, you can by writing a manifest file, and then running java -jar Yourjarfile.jar

Categories

Resources