代码之家  ›  专栏  ›  技术社区  ›  Jas

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

  •  0
  • Jas  · 技术社区  · 8 年前

    遵循本教程的“带字符串的管道”部分: http://www.jonathanbeard.io/tutorials/CtoJava

    我想修改streamtest代码,以便将数据保存到一个变量并将其传递给另一个类。

    我试过那样做(见下文),但当我跑 Java- CP。流线型试验 从教程中我了解到:

    在类streamtest中找不到main方法,请定义main方法 as:public static void main(字符串[]参数)

    我想这是有道理的,但我现在有点纠结于如何解决这个问题。

    主要思想是我希望能够从C代码中获取数据,将其放入变量pass(我猜是通过streamtest代码),然后将该变量传递给我的主笔记本类

    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;
    
    }
    }
    

    下面是我要将数据传递到的主类

    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);*/
    }
    }
    

    下面是未修改的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);
    
    }
    

    这是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;
    }
    

    这是制作文件

    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
    

    文本文件只是一堆数字

    我已经更新了streamtest代码并在eclipse中运行了它,但是我的输出是

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

    而不是

    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
    

    更新的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);
    
    }    
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   rohit thomas    8 年前

    你完全搞错了…你在跑

    爪哇

    因此,这是试图运行您的应用程序,因此错误,它找不到一个主方法,因为Java试图运行一个应用程序时寻找主方法…

    如果要保存数据,只需像这样将数据传递到命令中

    Java- CP。类“想要的字符串”

    然后在main方法中,让“string args[]从中读取:)

    编辑

    @你在干什么????你怎么能有两种主要的方法…?废料流测试仅使用主笔记本…

    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);*/
    }
    }
    

    如果你是从eclipse运行它 右键单击“运行”->“运行配置”->“参数” enter image description here

    输出将是“teenagevariant”,因为我们只取了args[0],如果您想要其他值,那么arg 1 精氨酸 2 …

    如果你想在命令行上运行它 enter image description here