Having problem while executing the main file - java

I am just a beginner trying to use a git java project, I have downloaded a java project from git and when I try to run the splash.java file it says:-
Class "Electricity.splash" does not have a main method
Here's the screenshot of the screen:-
Screenshot of my error
conn.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author Hp pc
*/
class conn {
}
screenshot of conn.java file in Electricity folder
screenshot
Here's my splash.java which has the main method:
package Electricity;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class splash {
public static void main(String[] args){
fframe f1 = new fframe();
f1.setVisible(true);
int i;
int x=1;
for(i=2; i<=600; i+=4, x+=1){
f1.setLocation(800 - ((i+x)/2), 500 - (i/2));
f1.setSize(i+x,i);
try{
Thread.sleep(10);
}catch(Exception e){}
}
}
}
class fframe extends JFrame implements Runnable{
Thread t1;
fframe(){
super("Electricity Billing System");
setLayout(new FlowLayout());
ImageIcon c1 = new ImageIcon(ClassLoader.getSystemResource("icon/elect.jpg"));
Image i1 = c1.getImage().getScaledInstance(730, 550,Image.SCALE_DEFAULT);
ImageIcon i2 = new ImageIcon(i1);
JLabel l1 = new JLabel(i2);
add(l1);
t1 = new Thread(this);
t1.start();
}
public void run(){
try{
Thread.sleep(7000);
this.setVisible(false);
Login l = new Login();
l.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
}
Please Guide me, is it because I haven't downloaded jdbc or something else?

First create your main method in the main class then, to specify the class with the main method: left click on the name of your project, properties, Run, and there specify the main class with the main method. In your capture: try, Import java.sql.Connection;

Related

sarxos webcam-capture-live-streaming example error: package us.sosia.video.stream.agent.ui does not exist

I want to make a webcam capture software and a stream software using sarxos's webcam library. Wanting to understand the examples first i don't know what to change or add in order to go past this error at import us.sosia.
package us.sosia.video.stream.agent.ui does not exist
package us.sosia.video.stream.handler
Maybe i have to make a Marvin project and change the pow.xml file but i don't know how to do this and still add my sarxos library to the Marvin project using NetBeans.
first class is StreamServer:
package us.sosia.video.stream.agent;
import java.awt.Dimension;
import java.net.InetSocketAddress;
import com.github.sarxos.webcam.Webcam;
public class StreamServer {
/**
* #author kerr
* #param args
*/
public static void main(String[] args) {
Webcam.setAutoOpenMode(true);
Webcam webcam = Webcam.getDefault();
Dimension dimension = new Dimension(320, 240);
webcam.setViewSize(dimension);
StreamServerAgent serverAgent = new StreamServerAgent(webcam, dimension);
serverAgent.start(new InetSocketAddress("localhost", 20000));
}
}
Second class is StreamClient:
package us.sosia.video.stream.agent;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.net.InetSocketAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.sosia.video.stream.agent.ui.SingleVideoDisplayWindow;
import us.sosia.video.stream.handler.StreamFrameListener;
public class StreamClient {
/**
* #author kerr
* */
private final static Dimension dimension = new Dimension(320,240);
private final static SingleVideoDisplayWindow displayWindow = new SingleVideoDisplayWindow("Stream example",dimension);
protected final static Logger logger = LoggerFactory.getLogger(StreamClient.class);
public static void main(String[] args) {
//setup the videoWindow
displayWindow.setVisible(true);
//setup the connection
logger.info("setup dimension :{}",dimension);
StreamClientAgent clientAgent = new StreamClientAgent(new StreamFrameListenerIMPL(),dimension);
clientAgent.connect(new InetSocketAddress("localhost", 20000));
}
protected static class StreamFrameListenerIMPL implements StreamFrameListener{
private volatile long count = 0;
#Override
public void onFrameReceived(BufferedImage image) {
logger.info("frame received :{}",count++);
displayWindow.updateImage(image);
}
}
}
I need a way to go past this error.
Thanks in advance.
your first 2 lines in both files are:
package us.sosia.video.stream.agent;
This means that these files should be in the following path
"/us/sosia/video/stream/agent/"
Remove this line from both files.

Instance "Main" Method Not Running When Called from Another Class

I'm learning Java and experimenting with Javafx in netbeans.
I am running the sqlite tutorial here: http://www.tutorialspoint.com/sqlite/sqlite_java.htm
When set up as a lone-file it works fine of course.
I'm setting it up in a test project "testDB" and for some reason when I initiate the class the class itself is recognized, but main() is not running.
Here is the testdb file itself:
testDB.java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sqlitetest;
import java.sql.*;
/**
*
*/
public class testDB {
public static void main(String args[]) {
//THESE STEPS ARE ON NOT RUNNING (compiles without errors)
System.out.println("testing");
Connection c = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
public void makeStuff(){
}
}
sqlitetest.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sqlitetest;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
*/
public class Sqlitetest extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
testDB test = new testDB();
test.makeStuff();
launch(args);
}
}
I think you are getting confused between a constructor and a main method.
A main method is only invoked when you start the JVM, running that specific class (or if you invoke it explicitly elsewhere).
A constructor is invoked when you create an instance of the class, like you are doing here.
In testdb, change:
public static void main(String args[]) {
to
public testdb() {
Alternatively, invoke testdb.main(args) (or with some other parameter) in Sqlitetest.main.

running a simple task example

Hi I've been trying all night to run this example and have had no luck what so ever, I cannot find a solution. I have two file.
First is Worker.java and here is its contents
import javafx.application.Application;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author brett
*/
public class Worker {
/**
* #param args the command line arguments
* #throws java.lang.Exception
*/
/**
*
* #param args
* #throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
doit();
}
private static void doit(){
try {
IteratingTask mytask = new IteratingTask(800000);
mytask.call();
System.out.println(mytask.getValue());
int pro = (int) mytask.getProgress();
System.out.println(pro);
} catch (Exception ex) {
Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Next is the IteratingTask.java file and its contents
//import javafx.concurrent.Task;
import javafx.application.Application;
import javafx.concurrent.Task;
/**
*
* #author brett
*/
public class IteratingTask extends Task<Integer> {
private final int totalIterations;
public IteratingTask(int totalIterations) {
this.totalIterations = totalIterations;
}
#Override protected Integer call() throws Exception {
int iterations;
// iterations = 0;
for (iterations = 0; iterations < totalIterations; iterations++) {
if (isCancelled()) {
updateMessage("Cancelled");
break;
}
updateMessage("Iteration " + iterations);
updateProgress(iterations, totalIterations);
}
return iterations;
}
}
I know I'm doing something very wrong but... I just cant see it.
Here is the error it get
run:
Jan 31, 2015 11:56:38 PM Worker doit
SEVERE: null
java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:270)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:265)
at javafx.application.Platform.runLater(Platform.java:81)
at javafx.concurrent.Task.runLater(Task.java:1211)
at javafx.concurrent.Task.updateMessage(Task.java:1129)
at IteratingTask.call(IteratingTask.java:24)
at Worker.doit(Worker.java:38)
at Worker.main(Worker.java:31)
BUILD SUCCESSFUL (total time: 0 seconds)
It builds ok.... any advice would be awesome.
The problem is that the FX Toolkit, and in particular the FX Application Thread have not been started. The update...(...) methods in Task update various state on the FX Application Thread, so your calls to those methods cause an IllegalStateException as there is no such thread running.
If you embed this code in an actual FX Application, it will run fine. Calling launch() causes the FX toolkit to be started.
Also, note that while this will run, Tasks are generally intended to be run in a background thread, as below:
import javafx.application.Application;
import javafx.scene.Scene ;
import javafx.scene.layout.StackPane ;
import javafx.scene.control.Label ;
import javafx.stage.Stage ;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Worker extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane(new Label("Hello World"));
Scene scene = new Scene(root, 350, 75);
primaryStage.setScene(scene);
primaryStage.show();
doit();
}
public static void main(String[] args) throws Exception {
launch(args);
}
private void doit(){
try {
IteratingTask mytask = new IteratingTask(800000);
// mytask.call();
Thread backgroundThread = new Thread(mytask);
backgroundThread.start(); // will return immediately, task runs in background
System.out.println(mytask.getValue());
int pro = (int) mytask.getProgress();
System.out.println(pro);
} catch (Exception ex) {
Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

Drag and Drop file path to Java Swing JTextField

Using this question, I created the class below, which handles drag and drop of files to a JTextField. The point of the application is to be able to drag a file into the text field, and have the text field's text set to the file's path (you can see the goal in the code pretty clearly).
My problem is the below code does not compile. The compilation error states Cannot refer to non-final variable myPanel inside an inner class defined in a different method. I haven't worked much with inner classes, so can seomeone show me how to resolve the error and get the code to behave as designed?
Code:
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent;
import java.io.File;
import java.util.List;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JTextArea myPanel = new JTextArea();
myPanel.setDropTarget(new DropTarget() {
public synchronized void drop(DropTargetDropEvent evt) {
try {
evt.acceptDrop(DnDConstants.ACTION_COPY);
List<File> droppedFiles = (List<File>) evt
.getTransferable().getTransferData(
DataFlavor.javaFileListFlavor);
for (File file : droppedFiles) {
/*
* NOTE:
* When I change this to a println,
* it prints the correct path
*/
myPanel.setText(file.getAbsolutePath());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
JFrame frame = new JFrame();
frame.add(myPanel);
frame.setVisible(true);
}
}
As the error message says, myPanel needs to be defined as final.
final JTextArea myPanel = new JTextArea();
This way the inner class can be given one reference pointer to the variable instance without concern that the variable might be changed to point to something else later during execution.
Another option is to declare the variable static.
static JTextArea myPanel = new JTextArea();

Simple Web Service adding two numbers

I have created a simple Webservice function as shown below;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ws;
import javax.jws.WebService;
/**
*
* #author Joe
*/
#WebService()
public class Add2Int {
public int add(int a, int b) {
return (a+b);
}
}
and I have created a very simple gui that allows the user to enter 2 numbers and which should output the result however this does not work? I tried it without the gui and it works but when i build the gui it does not work? here is my code for that side of things
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myjavawsclient;
//import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* #author Joe
*/
public class Calculator extends JFrame implements FocusListener {
JTextField value1 = new JTextField("", 5);
JLabel plus = new JLabel("+");
JTextField value2 = new JTextField("",5);
JLabel equals = new JLabel("=");
JTextField sum = new JTextField("", 5);
public Calculator() {
super("The Calculator");
setSize(350,90);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
setLayout(flow);
// add the listners
value1.addFocusListener(this);
value2.addFocusListener(this);
// set up sum field
sum.setEditable(true);
//add componets
add(value1);
add(plus);
add(value2);
add(equals);
add(sum);
setVisible(true);
}
public void focusGained(FocusEvent event){
try { // Call Web Service Operation
ws.Add2IntService service = new ws.Add2IntService();
ws.Add2Int port = service.getAdd2IntPort();
// TODO initialize WS operation arguments here
int result = 0;
int result2 = 0;
result = Integer.parseInt(value1.getText());
result2 = Integer.parseInt(value2.getText());
int total = port.add(result, result2);
sum.setText("" +total);
//float plusTotal = Float.parseFloat(value1.getText()) +
Float.parseFloat(value2.getText());
} catch (Exception ex) {
// TODO handle custom exceptions here
//value1.setText("0");
//value2.setText("0");
//sum.setText("0");
}
}
public void focusLost(FocusEvent event){
focusGained(event);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Calculator frame = new Calculator();
}
}
I am not getting any errors I am just not getting any result from the 2 numbers, for example 1+1=2 but with my application it allows the user to enter 1 + 1 = ? but where the question mark is nothing gets shown.
I was wondering if anyone could solve this problem for me. Oh and I am using NetBeans and GlassFish App server with WSDL
Joe
You should declare add as a webmethod.
try following:
#WebMethod public int add(int a, int b){
return (a+b);
}
My Fault! I forgot to start the App Server

Categories

Resources