简体   繁体   English

Java类:如何制作一个返回字符串参数但也可用作应用程序入口点的类?

[英]Java Class: How would I make a class that returns a string parameter but can also be used as an entry point for an application?

Following the "PIPE WITH STRINGS" section of this tutorial: http://www.jonathanbeard.io/tutorials/CtoJava 遵循本教程的“带有字符串的管道”部分: http : //www.jonathanbeard.io/tutorials/CtoJava

I want to modify the StreamTest code so that I can save the data to a variable and pass it to another class. 我想修改StreamTest代码,以便可以将数据保存到变量中并将其传递给另一个类。

I tried doing that (See Below) but when I run java -cp . 我尝试这样做(请参阅下文),但是当我运行java -cp时。 StreamTest from the tutorial I get this: 从教程的StreamTest我得到这个:

Main method not found in class StreamTest, please define the main method as: public static void main(String[] args) 在类StreamTest中找不到主要方法,请将该主要方法定义为:public static void main(String [] args)

Which makes sense I guess, but I am kind of stuck on how to approach this now. 我猜这是有道理的,但是我现在仍然对如何解决这个问题感到困惑。

The main idea is that I want to be able to get the data from the c code, put it in the variable pass (I guess through the StreamTest code), and then pass that variable to my mainLaptop class 主要思想是我希望能够从c代码中获取数据,将其放入变量传递(我想通过StreamTest代码),然后将该变量传递给我的mainLaptop类

import java.io.BufferedInputStream; 
import java.io.IOException;
import java.io.InputStream;

public class StreamTest
{
private static final int buffer = 4096;
public static String main(String [] args, String pass)
{

    InputStream is = null;
    BufferedInputStream bis = null;
    try
    {
        bis = new BufferedInputStream(System.in,buffer);
        StringBuilder sb = new StringBuilder();
        sb.append((char)bis.read());
        while(bis.available() > 0)
        {
            sb.append((char)bis.read());
        }

        System.out.println("JAVA SIDE: "+sb.toString());
        pass=sb.toString();
        bis.close();

    }
    catch(IOException ex){}
    finally{}
    //return pass;
    return pass;

}
}

Here is the main class I want to pass the data into 这是我想将数据传递到的主要类

public class mainLaptop 
{

public static void main(String arg) throws Exception 
{   
    //Timing out? change the IP!
    String ip="192.168.137.127";
    String Pi1Q1="Leonardo";
    String Pi1Q2="Raphael";
    String Pi2Q3="Donatello";
    String Pi2Q4="Michelangelo";
    String pass=arg;
    //pass= StreamTest.main(pass);

    Send.send(ip, Pi1Q1, pass);
    Send.send(ip, Pi1Q2, pass);
    Send.send(ip, Pi2Q3, pass);
    Send.send(ip, Pi2Q4, pass);

/*  Recv.recv(ip, Pi1Q1);
    Recv.recv(ip, Pi1Q2);
    Recv.recv(ip, Pi2Q3);
    Recv.recv(ip, Pi2Q4);*/
}
}

Here is the unmodified StreamTest that works 这是未修改的StreamTest

import java.io.BufferedInputStream; 
import java.io.IOException;
import java.io.InputStream;

public class StreamTest
   {
    private static final int buffer = 4096;

   public static void main(String[] args) throws Exception 
  {   
    String pass=null;
    InputStream is = null;
    BufferedInputStream bis = null;
    try
    {
        bis = new BufferedInputStream(System.in,buffer);
        StringBuilder sb = new StringBuilder();
        //sb.append((char)bis.read());
        while(bis.available() > 0){
            sb.append((char)bis.read());
        }
        pass = sb.toString();
        System.out.println("JAVA SIDE: "+sb.toString());
        bis.close();
    }
    catch(IOException ex)
    {

    }

    finally
    {


    }

  //  mainLaptop.main(pass);

}

Here is the c code 这是C代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>

#define DEBUG 0
#define BUFFER 4096

//open ap.txt for text input
static const char* exFile = "ap.txt";
static char inputBuffer[BUFFER];

int main(int argc, const char** argv)
{
   FILE *fp = fopen(exFile,"r");
   /*check and see if the pointer is null in otherwords see if the memory 
   location refered to by fp is set...no memory location should be zero 
   if you want to reference it   
   Here are some good ways to do this other than the way I did it below:
   if(!fp) {do error}
   if(fp == NULL) {do error}
   and then there's the way I did it below
   */

   if(fp == 0){
      fprintf(stderr,"Null pointer exception, check file name.\n");
      exit(-1);
   }

   //check and see if an error occured during open
   const int err = ferror(fp);
   if(err != 0){
      /*
     void perror(const char* err)
     returns specific error message to string attached.

     */
  const char* errMessage = strcat("Something bad happened while opening 
  file ",exFile);
  perror(errMessage);
   }
     #if (DEBUG == 1)   
    else
    {
      fprintf(stderr,"Success opening file!!\n");
    }  
    #endif




setbuf(fp,inputBuffer); //set a buffer for input

uint64_t *num = (uint64_t*) malloc(sizeof(uint64_t));
uint64_t total = 0;
uint64_t n = 0;

//test for eof
/*
feof(*fp) - returns a boolean true if at end of file and false otherwise
*/

while(!feof(fp)){
//fscanf returns the number of items it converted using %llu, if it's not 
 equal to 1 we don't want to continue
   if(fscanf(fp,"%"PRIu64"",num)!=1)
  break; //you could do a lot of stuff here as far as error handling but 
basically something bad has happened
   total+= *num; //add to total the value at memory location num
   n++;
    #if (DEBUG == 1)   
    fprintf(stderr,"line number %"PRIu64"\n",n);
    #endif 
    }

    free(num);

const double average = (double) total / (double) n;
//close the inputfile
fclose(fp);

//declare our outputfile, use a pipe in this case to a java process
//we open a java process for this process to pipe to, also it is 
//technically a bi-directional pipe so we can use any of the modifiers
//like r/w/r+/etc
static const char* outFile = "java -cp . StreamTest";

FILE *fp_out = popen(outFile,"w");
//setbuf(fp_out,outputBuffer);

fprintf(fp_out,"Total: %"PRIu64", Integers: %"PRIu64", Average: 
%.4f\n",total,n,average);



/*
int fflush(*fp) pushes any data in the buffer to be written
the return value returns 0 if successful or !=0 if an error 
occurs....remember return values in C often equal exceptions

*/   
   fflush(fp_out);

/*

int 

 */
    fclose(fp_out);

   return 1;
}

Here is the make file 这是make文件

CC ?=gcc
JCC ?= javac
FLAGS ?= -Wall -O2
JFLAGS ?= -g -verbose

all: c_app StreamTest

c_app: c_app.c
    $(CC) $(FLAGS) -o c_app c_app.c

StreamTest: StreamTest.java
    $(JCC) $(JFLAGS) StreamTest.java $(LIBS)

clean:
    rm -f c_app StreamTest.class

The ap.text file is just a bunch of numbers ap.text文件只是一堆数字

I've updated my StreamTest code and run it through eclipse but my output is 我已经更新了StreamTest代码并通过Eclipse运行它,但是我的输出是

JAVA SIDE: 
 [x] Sent ''Leonardo
 [x] Sent ''Raphael
 [x] Sent ''Donatello
 [x] Sent ''Michelangelo

instead of 代替

JAVA SIDE: 
 [x] Sent 'Total: 4953, Integers: 1000, Average: 4.9530'Leonardo
 [x] Sent 'Total: 4953, Integers: 1000, Average: 4.9530'Raphael
 [x] Sent 'Total: 4953, Integers: 1000, Average: 4.9530'Donatello
 [x] Sent 'Total: 4953, Integers: 1000, Average: 4.9530'Michelangelo

Updated StreamTest 更新了StreamTest

import java.io.BufferedInputStream; 
import java.io.IOException;
import java.io.InputStream;

public class StreamTest
{
private static final int buffer = 4096;

public static void main(String[] args) throws Exception 
{
    String pass=null;
    InputStream is = null;
    BufferedInputStream bis = null;
    try
    {
        bis = new BufferedInputStream(System.in,buffer);
        StringBuilder sb = new StringBuilder();
        //sb.append((char)bis.read());
        while(bis.available() > 0){
            sb.append((char)bis.read());
        }
        pass = sb.toString();
        System.out.println("JAVA SIDE: "+pass);
        bis.close();
    }
    catch(IOException ex)
    {

    }

    finally
    {


    }
    //pass = "hi";
    mainLaptop.main(pass);

}    
}

You've got it all wrong ... you are running 一切都错了...您正在跑步

java -cp java -cp

So this is trying to run your application, hence the error, that it cant find a Main method because Java looks for a Main method when trying to run an application... 因此,这是尝试运行您的应用程序的错误,因此它无法找到Main方法,因为Java在尝试运行应用程序时会查找Main方法。

If you want to save the data just pass the data into the command like so 如果要保存数据,只需将数据传递到命令中,如下所示

java -cp . java -cp。 class "the string you want" 类“您想要的字符串”

And then in the main method you have the "String args[]" read it from that :) 然后在main方法中,您需要从“ String args []”中读取它:)

EDITED EDITED

@Jas Buddy what are you doing ??? @Jas Buddy你在做什么??? how can you have two main Method ....? 你怎么有两个主要的方法....? scrap StreamTest use only mainLaptop .... 废弃StreamTest仅使用mainLaptop...。

public class mainLaptop 
{

public static void main(String arg) throws Exception 
{   
    //Timing out? change the IP!
    String ip="192.168.137.127";
    String Pi1Q1="Leonardo";
    String Pi1Q2="Raphael";
    String Pi2Q3="Donatello";
    String Pi2Q4="Michelangelo";
    String pass=arg[0]; // reads the argument you pass from command line or eclipse
    //pass= StreamTest.main(pass);

    Send.send(ip, Pi1Q1, pass);
    Send.send(ip, Pi1Q2, pass);
    Send.send(ip, Pi2Q3, pass);
    Send.send(ip, Pi2Q4, pass);

/*  Recv.recv(ip, Pi1Q1);
    Recv.recv(ip, Pi1Q2);
    Recv.recv(ip, Pi2Q3);
    Recv.recv(ip, Pi2Q4);*/
}
}

IF you are running it from eclipse Right click run-->Run configuration--> Arguments 如果您从eclipse运行它,请右键单击运行->运行配置->参数 在此处输入图片说明

Output will be "teenagemutant" because we have taken only args[0], if you want the other values then arg 1 ,args 2 ... 输出将是“ teenagemutant”,因为我们仅采用了args [0],如果您需要其他值,则arg 1 ,args 2 ...

if you want to run it on commandLine then 如果您想在命令行上运行它 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为JBCO(Java ByteCode Obfuscator)指定入口点类和jar存档? - How can I specify entry-point class and jar archive for JBCO (Java ByteCode Obfuscator)? 在 Java 中,我将如何制作一个 class 本质上是字符串的子类,但设置允许的长度范围? - In Java, how would I make a class that is essentially a subclass of string, but sets an allowable length range? 我如何使Java类线程对于XPages安全 - How would I make a Java class thread safe for XPages 在 Java 中,我能否重用接口参数中的 generics 类型来创建不同的 class,它也需要泛型类型? 如果是这样,怎么办? - In Java, can I reuse the generics types from an interface parameter to create a different class which also requires generic types? And if so, how? 爪哇。 getClass() 返回一个类,我怎么也能得到一个字符串? - Java. getClass() returns a class, how come I can get a string too? 如何在子类中使用子类中的变量 - How can I make the variable from a subclass be used in main class 在Java中,如何实现返回Class的方法 <List<String> &gt; - In Java, how do I implement a method that returns Class<List<String>> 如何使用作为命令参数传输的 String 变量到 Java 中另一个类的实例? - How can I use the String variable transferred as the command parameter to a instance of another class in Java? 如何使用也键入的类型参数创建java.lang.Class? - How to create java.lang.Class with type parameter that is also typed? 如何将 http 入口点设置为我的 Java 应用程序 - How do I set http entry point to my java application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM